VBGood网站全文搜索 Google

首页 - 经验之谈 - 用VB轻松打开MS Word文档
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:11936, 日期:2001-08-01
Effortlessly open Microsoft Word documents from VB

Often, your application may have cause to open a Microsoft Word
document. Rather than accessing it via some directory method, such
as the FileSystemObject or Shell, try opening it directly via Word
Automation. This approach will make it that much easier to read
and manipulate the document注释:s properties and content.

To open a Word document via Automation, you must first work your
way down the object hierarchy to the Documents collection, which
is a member of the top-level Application object. As you can guess,
the Documents collection holds all open documents, and exposes
an Open method. To use this method you simply pass in the full
path of the document you wish to open. The Open method in turn
returns a Document object that represents the document you requested.
Once obtained, you can then read and manipulate the file注释:s contents
as you see fit. The following code shows an example:

Private Sub Command1_Click()
Dim appWord As Word.Application
Dim wrdDoc As Word.Document
Dim strFileName As String
strFileName = "D:\Articles\Title.doc"
Set appWord = New Word.Application
Set wrdDoc = appWord.Documents.Open(strFileName)
MsgBox wrdDoc.Path & "\" & wrdDoc.Name
wrdDoc.Close False
Set wrdDoc = Nothing
Set appWord = Nothing
End Sub