Application :用来表现WORD应用程序,包含其它所有对象。他的成员经常应用于整个WORD,你可以用它的属性和方法控制WORD环境。
Document :Document对象是WORD编程的核心。当你打开打开一个已有的文档或创建一个新的文档时,就创建了一个新的Documnet对象, 新创建的Document将会被添加到Word Documents Collection。
Selection :Selection对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。
Rang :是Document的连续部分,根据起始字符和结束字符定义位置。
Bookmark:类似于Rang,但Bookmark可以有名字并在保存Document时Bookmark也被保存。
以下代码则为打开一个WORD2003文件。
public void CreateWordDocument(string FileName)
{
if(FileName == "") return;
this.thisApplication =
new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
// Microsoft.Office.Interop.Word.DocumentClass wordDoc =
// wordApp.Documents.Open(ref filename, ref ConfirmConversions,
// ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
// ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
// ref Encoding, ref Visible);
// Microsoft.Office.Interop.Word.DocumentClass wordDoc =
// wordApp.Documents.Open(ref filename, ref ConfirmConversions,
// ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
// ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
// ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog);
Microsoft.Office.Interop.Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform );
this.thisDocument = wordDoc;
formFields = wordDoc.FormFields;
}
关闭WORD程序程序
Object SaveChanges = false;
Object OriginalFormat = System.Type.Missing;
Object RouteDocument = System.Type.Missing;
this.thisApplication.Quit( ref SaveChanges, ref OriginalFormat, ref RouteDocument );
参考资料:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/wordobject.asp