2013-02-15 112 views
2

我在使用嵌入式Word文件執行郵件合併時遇到了一些問題。從本質上講編輯後關閉嵌入對象

我試圖建立一個宏:

  • 打開嵌入的Word文件(假設該文件是對象(2))
  • 導出表到單詞呸連接
  • 預覽結果
  • 更新表的內容
  • 文件導出爲PDF
  • 關閉Word應用程序留下完成PD F開

下面是我高達到目前爲止的代碼,但它不會事後關閉WINWORD應用:

Public Sub fExportSVF() 

    Dim WdObj As Object 
    Dim WdApp As Word.Application 
    Dim WdDoc As Word.Document 
    Dim intIndex As Integer 
    Dim strPeril As String 
    Dim strClaimNumber As String 
    Dim strPHName As String 
    Dim strSaveLoc As String 
    Dim strWbName As String 
    Dim strTempLoc As String 
    Dim xlObj As Object 

    Application.ScreenUpdating = False 

    Call fUnhideSheet("EXPORT_DATA") 

    Application.DisplayAlerts = False 
    ThisWorkbook.Save 
    Application.DisplayAlerts = True 

    strTempLoc = Environ("TEMP") & Int((9999 - 1 + 1) * Rnd + 1) & ".xlsm" 

    strWbName = Worksheets("Settings").Range("B4").Value 
    strPeril = Worksheets("Settings").Range("B3").Value 
    strClaimNumber = Worksheets("Settings").Range("B1").Value 
    strPHName = Worksheets("Settings").Range("B2").Value 

    If Dir(strTempLoc) <> "" Then Kill strTempLoc 

    Set xlObj = CreateObject("Scripting.FileSystemObject") 

    xlObj.CopyFile ThisWorkbook.FullName, strTempLoc, True 

    strSaveLoc = ActiveWorkbook.Path & "\" & strClaimNumber & _ 
    " - " & strPHName & " - " & strPeril & ".pdf" 

    Select Case strPeril 
     Case "Acc" 
      intIndex = 2 
     Case "Acci" 
      intIndex = 3 
     Case "AD" 
      intIndex = 4 
     Case "Es" 
      intIndex = 5 
     Case "Fi" 
      intIndex = 6 
     Case "Fld" 
      intIndex = 7 
     Case "Impt" 
      intIndex = 8 
     Case "St" 
      intIndex = 9 
     Case "Th" 
      intIndex = 10 
    End Select 

    Set WdObj = Worksheets("Settings").OLEObjects(intIndex) 

    WdObj.Activate 
    WdObj.Object.Application.Visible = False 

    Set WdApp = GetObject(, "Word.Application") 
    Set WdDoc = WdApp.ActiveDocument 

    WdApp.Visible = True 

    WdDoc.MailMerge.MainDocumentType = wdFormLetters 

    WdDoc.MailMerge.OpenDataSource Name:= _ 
     strWbName _ 
     , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _ 
     AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _ 
     WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _ 
     Format:=wdOpenFormatAuto, Connection:= _ 
     "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & strTempLoc & _ 
     ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet " & _ 
     "OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine " _ 
     , SQLStatement:="SELECT * FROM `EXPORT_DATA$`", SQLStatement1:="", _ 
     SubType:=wdMergeSubTypeAccess 
    WdDoc.MailMerge.ViewMailMergeFieldCodes = wdToggle 

    WdDoc.TablesOfContents(1).Update 

    WdDoc.ExportAsFixedFormat outputfilename:=strSaveLoc, _ 
     ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _ 
     wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _ 
     Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _ 
     CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _ 
     BitmapMissingFonts:=True, UseISO19005_1:=False 

    WdApp.ActiveDocument.Close wdDoNotSaveChanges 

    Set WdApp = Nothing 
    Set WdObj = Nothing 

    Kill strTempLoc 

    Call fHideSheet("EXPORT_DATA") 

    Application.ScreenUpdating = True 

End Sub 

所以它做的一切除了關閉WINWORD應用。除此之外,我注意到,如果還有另一個文檔已經打開,它也會使其不可見。

請幫忙嗎?

乾杯

編輯:另外,該代碼將在運行的機器將同時擁有Word 97和Word 2007在他們身上。該文件將需要打開並在Word中編輯2007感謝

回答

0

改變這一行(設置WdApp到Word的現有實例,如果有的話):

Set WdApp = GetObject(, "Word.Application")

這一點,它創建了一個Word中的新實例,以防止關閉可能是打開其他文件:

Set WdApp = CreateObject("Word.Application")

要關閉wdApp使用Quit方法:

wdApp.Quit