Microsoft Word 对象(4) defaultDir = Options. DefaultFilePath (wdDocumentsPath)With Application. FileSearch.FileName = "Test.doc".LookIn = defaultDir.ExecuteIf .FoundFiles. Count = 1 ThenDocuments. Open FileName :=defaultDir & Application. PathSeparator & "TEST.DOC"ElseMsgBox "Test.doc file was not found"End IfEnd With也可以允许用户选择要打开的文件,而不是定死Open方法的FileName参数值。如同以下示例所示的那样,可以使用带有wdDialogFileOpen常量的Dialogs属性来返回一个Dialog 对象,该对象表示“打开”对话框(在“文件”菜单中)。Show方法可以显示并且执行在“打开”对话框中完成的动作。Dialogs (wdDialogFileOpen). ShowDisplay 方法只是用来显示特定的对话框而不作任何更多的操作。以下的示例检查Display方法的返回值。如果用户单击“确定”来关闭对话框,返回值-1并且打开所选择的文件,该文件的名字保存在变量fSelected中。Set dlg = Dialogs (wdDialogFileOpen)aButton = dlg. DisplayfSelected = dlg. NameIf aButton = -1 ThenDocuments. Open FileName :=fSelectedEnd If要对如何显示Word对话框作进一步了解,请参阅“帮助”中的“显示内置Word 对话框”一节的内容。要判断某个特殊的文档是否打开,可以使用一条For Each…Next语句来对Documents集合进行列举。如果名为“Sample.doc”的文档已经被打开了,以下的示例就激活它;如果它尚未被打开,则该示例会打开它。docFound = TrueFor Each aDoc In DocumentsIf InStr (1, aDoc. Name, "sample.doc", 1) Then aDoc. ActivateExit ForElsedocFound = FalseEnd IfNext aDocIf docFound = False Then Documents. Open _FileName :="C:\Documents\Sample.doc"可以使用Count属性来确定当前打开文档的数目。Count属性应用于Documents集合,可以使用Document属性返回该集合。如果没有已打开的文档,以下的示例会显示一条消息。If Documents. Count = 0 Then MsgBox "No documents are open"创建和保存文档要创建一个新文档,可以对Documents集合应用Add 方法。以下的示例创建了一个新文档。Documents. AddAdd方法返回了仅作为一个Document对象而创建的文档。当用户添加一个文档时,可以设置Add 方法的返回值是一个对象变量,以便用户可以在自己的代码中引用该新文档。以下的示例创建了一个新文档,并且设置它的上边距为1.25英寸。Dim myDoc As DocumentSet myDoc = Documents. Add