曾经编译过一篇《用Visual Studio .NET自动化MS Word》,那是基于Word2000的,其实由于我跟这方面打交道不是太多,所以也就没有继续研究过了,昨天一位园友针对那文章提了个问题“我现在需要的是把一个文档的内容拷到另外一个文档内容之后,先把第一个文档调动内容复制到剪贴板上了,但下面应该怎么把粘贴位置定位到另一个文档之后呢?”。没有办法,我只能重新拿出那些代码,但我现在的是word2003了,所以编译代码是需要修改的了,所以针对Word2003的一些东西,在写点东西,希望对正想做这类事情又无从下手的朋友一点帮助。
注意:这是Office2003的,如果是其它版本则就不同了。
先创建一个MS Word COM对象,该对象是我们用来访问Word提供的各种函数,如果你想明白哪些函数是可以用的,你最好去Word中查找,打开Word,按[Alt+F11],然后再按F1,你就可以打开帮助了。
Word.ApplicationClass vk_word_app = new Word.ApplicationClass();
vk_word_app.Visible = true;
vk_word_app.Activate();
上面的vk_word_app就是一个Word COM对象,并激活了它,且显示出来,如果设为false,则word在后台运行,看不到它的界面。
object fileName = openFileDialog.FileName;
object vk_ConfirmConversions = true;
object vk_ReadOnly = false;
object vk_AddToRecentFiles = true;
object vk_PasswordDocument = "";
object vk_PasswordTemplate = "";
object vk_Revert = false;
object vk_WritePasswordDocument= "";
object vk_WritePasswordTemplate= "";
object vk_Format = Word.WdOpenFormat.wdOpenFormatAuto;
object vk_Encoding = System.Reflection.Missing.Value;
object vk_Visible = true;
object vk_OpenConflictDocument = false;
object vk_OpenAndRepair = true;
object vk_DocumentDirection = Word.WdDocumentDirection.wdLeftToRight;
object vk_NoEncodingDialog = true;
object vk_XMLTransform = System.Reflection.Missing.Value;
object vk_false = false;
object vk_true = true;
object vk_dynamic = 2;
object vk_missing = System.Reflection.Missing.Value;
StartWord();
// 打开文档
vk_my_doc = vk_word_app.Documents.Open(ref fileName,
ref vk_ConfirmConversions, ref vk_ReadOnly, ref vk_AddToRecentFiles, ref vk_PasswordDocument,
ref vk_PasswordTemplate, ref vk_Revert, ref vk_WritePasswordDocument, ref vk_WritePasswordTemplate,
ref vk_Format, ref vk_Encoding, ref vk_Visible,ref vk_OpenConflictDocument,ref vk_OpenAndRepair, ref vk_DocumentDirection, ref vk_XMLTransform);
其实这中有许多参数都是用处不大,但为了大家看得更加明白,我还是一个一个写得比较清楚,比较容易明白参数是什么意思(这些参数什么意思,请按上面提到的方法去Word中找)。
下面的代码告诉我们如果建立一个新的word文档,而且向文档中加入一些内容:
Word.Document vk_new_doc = vk_word_app.Documents.Add( ref vk_missing,
ref vk_missing, ref vk_missing, ref vk_Visible );
doc.Select();
vk_word_app.Selection.Copy();
// Paste into new document as unformatted text
vk_new_doc.Select();
vk_word_app.Selection.PasteSpecial( ref vk_missing, ref vk_false,
ref vk_missing, ref vk_false, ref vk_DataType, ref vk_missing, ref vk_missing );
刚才新建一个文档,现在要保存它,并且关闭它:
object vk_LockComments = false;
object vk_FileFormat = Word.WdSaveFormat.wdFormatDocument;
object vk_Password = "";
object vk_AddToRecentFiles = true;
object vk_WritePassword = "";
object vk_ReadOnlyRecommended = false;
object vk_EmbedTrueTypeFonts = true;
object vk_SaveNativePictureFormat = true;
object vk_SaveFormsData = false;
object vk_SaveAsAOCELetter = true;
object vk_Encoding = System.Reflection.Missing.Value;
object vk_nsertLineBreaks = false;
object vk_AllowSubstitutions = false;
object vk_LineEnding = Word.WdLineEndingType.wdCRLF;
object vk_AddBiDiMarks = false;
vk_new_doc.Content.InsertAfter("这个是我新建的文档!!!");
// 保存文档
vk_new_doc.SaveAs(ref vk_FileName, ref vk_FileFormat,ref vk_LockComments, ref vk_Password,ref vk_AddToRecentFiles, ref vk_WritePassword,
ref vk_ReadOnlyRecommended, ref vk_EmbedTrueTypeFonts, ref vk_SaveNativePictureFormat, ref vk_SaveFormsData, ref vk_SaveAsAOCELetter, ref vk_Encoding,
ref vk_nsertLineBreaks,ref vk_AllowSubstitutions,ref vk_LineEnding,ref vk_AddBiDiMarks);
// 关闭新文档
object vk_SaveChanges = Word.WdSaveOptions.wdSaveChanges;
object vk_OriginalFormat = Word.WdOriginalFormat.wdWordDocument;
object vk_RouteDocument = false;
doc.Close( ref vk_SaveChanges, ref vk_OriginalFormat, ref vk_RouteDocument );
vk_new_doc.Close( ref vk_SaveChanges, ref vk_OriginalFormat, ref vk_RouteDocument );
查找和替换是常用的功能,我的代码中就是使用如下方法:
private void FindAndReplace( object FindText, object ReplaceWith, object Wrap )
{
object vk_FindText = FindText;
object vk_MatchCase = false;
object vk_MatchWholeWord = false;
object vk_MatchWildcards = false;
object vk_MatchSoundsLike = false;
object vk_MatchAllWordForms = false;
object vk_Forward = true;
object vk_Wrap = Wrap;
object vk_Format = false;
object vk_ReplaceWith = ReplaceWith;
object vk_Replace = Word.WdReplace.wdReplaceAll;
object vk_MatchKashida = false;
object vk_MatchDiacritics = false;
object vk_MatchAlefHamza = false;
object vk_MatchControl = false;
vk_word_app.Selection.Find.Execute( ref vk_FindText, ref vk_MatchCase, ref vk_MatchWholeWord,
ref vk_MatchWildcards, ref vk_MatchSoundsLike, ref vk_MatchAllWordForms, ref vk_Forward, ref vk_Wrap, ref vk_Format,
ref vk_ReplaceWith, ref vk_Replace, ref vk_MatchKashida, ref vk_MatchDiacritics, ref vk_MatchAlefHamza, ref vk_MatchControl );
}
这就是那个园友问的,顺便也在这贴出:
string content = string.Empty;
try
{
IDataObject idata = Clipboard.GetDataObject();
if(idata.GetDataPresent(DataFormats.Text))
{
content = (string)idata.GetData(DataFormats.Text);
this.textBox1.Text = content;
}
else
{
this.textBox1.Text = "现在剪贴板上没有任何东西!";
}
}
catch(Exception ex)
{
this.textBox1.Text = ex.Message;
}
object vk_Direction = Word.WdCollapseDirection.wdCollapseStart;
doc.Content.InsertParagraphAfter();
Word.Range range = doc.Paragraphs[doc.Paragraphs.Count].Range;
range.Collapse(ref vk_Direction);
range.Paste();
这样的结果就是在文档的最后,重新开始一段并把内容加进去。
如果你要批量的给文档设定密码,可以稍微改造如下方法就可以办到,非常简单:
doc.Password = txtPassword.Text;
CloseDoc(doc);
vk_word_app.Quit( ref vk_false, ref vk_missing, ref vk_missing );
其实用vs2003操作对Word编程也不是很难的,只是参数还是不少,要想用好,那就靠多动手了。写得很简单,因为前面编译的那篇《用Visual Studio .NET自动化MS Word》中已经很详细了。如果再有不明白,去查Word或者看我的源码。
http://sifang2004.cnblogs.com/archive/2006/06/15/426773.html