Quit Word.Application which opened in the child subrotine

瘦欲@ 提交于 2019-12-02 17:17:51

问题


We declared Word.Application object in the Pro1 child function below. As we need return the Word.Document object, that should be remained opened. How can I do quit the Word application in the parent SubRoutuine(test01)


I need the objWord object which declared in Pro1 function will quit -quietly without prompt- after the test01 procedure running have end.

I have two procedure below

And perform this:

Sub test01()
    WrdPDF objDoc:=Pro1 strPath:=ThisWorkbook.path & "\" & "rep.pdf"
End Sub

|

Function Pro1 As Word.Document

    'Declaration
    Dim objWord As Word.Application
    ' Here we declare a Word Application in the function and need 
    ' quit that in another procedure (The parent one which is test01). 
    Dim objDocTotal As Word.Document
    Dim objDoc As Word.Document

    'Initializing
    Worksheets("Salary").OLEObjects("PayCheck").Activate
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = False
    Set objDoc = objWord.ActiveDocument
    Set objDocTotal = objWord.Documents.Add

...

    Proc1 = objDocTotal
End Function

|

Sub WrdPDF(objDoc As Object, strPath As String, Optional Opn As Boolean) 'MS-Word PDF
    objDoc.ExportAsFixedFormat _
        outputfileName:=strPath _
        , exportformat:=wdExportFormatPDF _
        , openafterexport:=Opn _
        , optimizefor:=wdExportOptimizeForPrint _
        , Range:=wdExportAllDocument _
        , Item:=wdExportDocumentContent _
        , includedocprops:=False _
        , keepirm:=True _
        , createbookmarks:=wdExportCreateNoBookmarks _
        , docstructuretags:=True _
        , bitmapmissingfonts:=True _
        , useiso19005_1:=False
End Sub

Regards.


回答1:


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.



来源:https://stackoverflow.com/questions/48475673/quit-word-application-which-opened-in-the-child-subrotine

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