Quit Word.Application which opened in the child subrotine

北战南征 提交于 2019-12-02 13:03:12

You can't Quit in that sub-routine and leave a document open. Once that subroutine ends, the variable objWord is no longer available - you've "orphaned" the instance of Word.

I see two possibilities, right off-hand, both of which involve passing and getting an object back from Proc1 so that you can access the application object:

1) You can declare the Word.Application object in test_01 and pass it to Proc1

Sub test01()
    Dim objWord as Word.Application
    WrdPDF objDoc:=Proc1(objWord), strPath:=ThisWorkbook.path & "\" & "rep.pdf"
    objWord.Quit 0
End Sub

Function Proc1(ref objWord as Word.Application) As Word.Document
    Dim objDocTotal As Word.Document
    Dim objDoc As Word.Document
    'And so on, as you currently have, without declaring objWord here

2) You can use access the application object via the document object returned by your Proc1:

Sub test01()
    Dim objDoc as Word.Document
    Dim objWord as Word.Application
    Dim strPath as String

    strPath = ThisWorkbook.path & "\" & "rep.pdf"
    Set objDoc = Proc1
    WrdPDF objDoc, strPath
    Set objWord = objDoc.Application
    objWord.Quit 0 'do not prompt to save changes
End Sub

I'd probably tend to use (2), but both work when I test them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!