最初,我在编写此文件时有帮助,它是在Windows 7机器上编写的,并且运行良好。我在Mac(OSX10.11.6)上安装了Office 2016,但该宏在Mac上不起作用。
该宏的目的是从邮件合并doc模板中打开一个新文档,保存新打开的窗口,关闭该新窗口,前进到模板上的下一条记录,然后重复直到到达邮件末尾合并收件人列表(在excel中)。
当前,它将打开一个新窗口,但不会保存并前进到下一条记录,显示找不到命名参数对话框。调试器将此行显示为罪魁祸首:
SaveAsAOCELetter:= False,CompatibilityMode:= 14
我删除了, CompatibilityMode:=14
,宏可以打开新窗口,保存文件,关闭窗口并前进到模板上的下一条记录,但没有重复该过程。
如何使宏不引发错误并前进到下一条记录并重复该过程?
Sub Start()
Dim i As Long
For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
flet1
Next i
End Sub
Sub flet1()
'
' flet1 Makro
' 1) Merges active record and saves the resulting document named by the
datafield FileName"
' 2) Closes the resulting document, and (assuming that we return to the
template)
' 3) advances to the next record in the datasource
Dim DokName As String 'ADDED CODE
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
' Remember the wanted documentname
DokName = .DataFields("FileName").Value ' ADDED CODE
End With
' Merge the active record
.Execute Pause:=False
End With
' Save then resulting document. NOTICE MODIFIED filename
ActiveDocument.SaveAs2
FileName:="/Users/username/Desktop/foldername/foldername2/foldername3/" +
DokName + ".docx", FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="",
AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14
' Close the resulting document
ActiveWindow.Close
' Now, back in the template document, advance to next record
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
End Sub