退出在子subrotine中打开的Word

退出在子subrotine中打开的Word

本文介绍了退出在子subrotine中打开的Word.Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在下面的Pro1子功能中声明了Word.Application对象.当我们需要返回Word.Document对象时,该对象应保持打开状态.我该如何退出父SubRoutuine(test01)

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)

我需要Pro1函数中声明的objWord对象,该对象将在test01过程运行结束后无提示地退出.

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

我下面有两个步骤

并执行以下操作:

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

致谢.

推荐答案

您无法退出该子例程并使文档保持打开状态.一旦该子例程结束,变量objWord将不再可用-您已孤立"了Word的实例.

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.

我立即看到了两种可能性,它们都涉及从Proc1传递和获取对象,以便您可以访问应用程序对象:

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)您可以在test_01中声明Word.Application对象,并将其传递给Proc1

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)您可以通过Proc1返回的文档对象来访问应用程序对象:

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

我可能倾向于使用(2),但是当我测试它们时都可以使用.

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

这篇关于退出在子subrotine中打开的Word.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:19