停用OLEObject

时间:2018-01-29 10:25:12

标签: excel excel-vba ms-word word-vba vba

我有:

Dim oDoc As OLEObject
Set oDoc = WS.OLEObjects("WordDoc")
oDoc.Activate
...

我如何制作oDoc.Deactivate? (所以EndTask是WINWORD.EXE)

1 个答案:

答案 0 :(得分:0)

Sub WorkWithOLEWordDoc( )

    'Declaration
    Dim WS As Worksheet
    Dim oDoc As OLEObject

    'Initializing
    Set Ws = WorkSheets("Sheet1")

    Set oDoc = WS.OLEObjects("WordDoc")
    oDoc.Activate

    'Codes works with the activated OLE Document

    'Deactivating
    Dim DCount as Integer
    DCount = 0
    On Error Resume Next
    DCount = Word.Documents.Count
    If Not Err = 0 Then Word.ActiveDocument.Close Else _
        If DCount > 1 Then Word.ActiveDocument.Close Else Word.Application.Quit

    set oDoc = Nothing

End Sub 'WorkWithWordDoc