保存为 .docm 之前/之后宏消失了

保存为 .docm 之前/之后宏消失了

我正在处理一个 excel 文件,它打开一个源文档 (word),我想将其保存为另一个名称,但要使用源中的宏。
打开文件不是问题,但在合并(我使用邮件合并)并更改源文档名称后,宏就消失了(甚至在保存为 .docm 之前)。不过,在源文档中,合并后宏可用。

任何想法如何解决这一问题?

我使用的代码是这样的:

Dim wd As Object
Dim wdocSource As Object
Dim strWorkbookName As String
Dim DocSrc  As String
Dim number As String

number = CStr(ActiveWorkbook.Sheets("MailMerge").Cells(2, 5))

Const wdFormLetters = 0, wdOpenFormatAuto = 0
Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = 1

'If Len(Dir("<directory>" & number, vbDirectory)) = 0 Then MkDir "<directory>" & number
fileSaveName = "X:\SAVENAME.docm"
On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdocSource = wd.Documents.Open("<directroy>\Test.docm")

strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name

wdocSource.MailMerge.MainDocumentType = wdFormLetters

wdocSource.MailMerge.OpenDataSource _
        Name:=strWorkbookName, _
        AddToRecentFiles:=False, _
        Revert:=False, _
        Format:=wdOpenFormatAuto, _
        Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
        SQLStatement:="SELECT * FROM `MailMerge$` "
          With wdocSource.MailMerge
              .Destination = wdSendToNewDocument
              .SuppressBlankLines = True
                With .DataSource
                     .FirstRecord = wdDefaultFirstRecord
                     .LastRecord = wdDefaultLastRecord
                End With
              .Execute Pause:=False
          End With

   wd.Visible = True

 ' wdocSource.Close SaveChanges:=False
  'wd.ActiveDocument.SaveAs fileSaveName
  'wd.ActiveDocument.Close
Set wdocSource = Nothing
Set wd = Nothing

相关内容