gpt4 book ai didi

excel - 编辑后关闭嵌入对象

转载 作者:行者123 更新时间:2023-12-04 21:06:36 25 4
gpt4 key购买 nike

我在使用嵌入的 Word 文件执行邮件合并时遇到了一些问题。本质上

我正在尝试构建一个宏来:

  • 打开一个嵌入的 Word 文件(假设文件是​​ Object(2))
  • 将导出表连接到单词 fie
  • 预览结果
  • 更新目录
  • 将文件导出为 PDF
  • 关闭 word application 使完整的 PDF 打开

  • 这是我到目前为止的代码,但之后它不会关闭 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 中打开和编辑。

    最佳答案

    更改此行(将 WdApp 设置为现有的 Word 实例,如果有的话):
    Set WdApp = GetObject(, "Word.Application")
    为此,它会创建一个新的 Word 实例,以防止关闭其他可能打开的文档:
    Set WdApp = CreateObject("Word.Application")
    关闭wdApp使用退出方法:
    wdApp.Quit

    关于excel - 编辑后关闭嵌入对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14894213/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com