您可以使用后期绑定 ( http://support.microsoft.com/kb/245115 ) 和GetObject
. 如果您打开了多个 Word 实例,则不能保证您会获得其中的任何一个。
获取 Word 实例将允许您访问ActiveDocument
应用程序的当前Selection
. 我仍然建议做一些错误检查,以确保你得到了你想要的。
Sub GetWordDocument()
Dim wdApp As Object
'Turn off error handling since if the Application is not found we'll get an error
'Use Late Binding and the GetObject method to find any open instances of Word
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
'Check to see if we found an instance. If not you can create one if you desire
If wdApp Is Nothing Then
MsgBox "No instances of Word found"
Exit Sub
End If
'Check if there are documents in the found instance of Word
If wdApp.Documents.Count > 0 Then
wdApp.Selection.TypeText "Cool, we got it" & vbCr
'You can now access any of the active document properties too
wdApp.ActiveDocument.Range.InsertAfter "We did indeed"
End If
'Clean up the Object when Finished
Set wdApp = Nothing
End Sub