我希望能够保存Word 2010 MailMerge中的活动文件,其文件名派生自数据库字段"First_Name"和数据库字段"Last_Name"到一个子文件夹,这是一个硬编码的子文件夹。
我收到一个错误,"requested member of the collection does not exist"
。
我知道当你试图访问一个不存在的对象时会发生这个错误。数据库字段是First_Name,我也尝试过First Name,以防代码正在搜索First Name的地址字段,该地址字段已与First_Name的数据库字段配对。以下是我尝试过的:
Sub SavingIndividuallyByCustomerName()
Dim firstname As String
Dim lastname As String
firstname = ActiveDocument.FormFields("First_Name").Result
lastname = ActiveDocument.FormFields("Last_Name").Result
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
End With
.Execute Pause:=False
End With
ChangeFileOpenDirectory "C:foldersubfoldersubsubfolder"
ActiveDocument.SaveAs2 FileName:= _
"C:foldersubfoldersubsubfolder" & firstname & lastname & ".docx"
End Sub
当我用
硬编码名称时firstname = "John"
lastname = "Doe"
我没有其他错误,活动文件保存。
我也尝试使用但没有成功:
Dim firstname As Field
Dim lastname As Field
代替
firstname = ActiveDocument.FormFields("First_Name").Result
lastname = ActiveDocument.FormFields("Last_Name").Result
你需要firstname = ActiveDocument.MailMerge.DataSource.DataFields("First_Name").Value
lastname = ActiveDocument.MailMerge.DataSource.DataFields("Last_Name").Value
引号中的名称区分大小写(在Windows编程中不常见)。它必须匹配Word实际使用的名称,该名称可能与数据源中的名称不相同。