Question

I would like to able to save the active file in Word 2010 MailMerge, with its file name derived from the database field "First_Name" and from the database Field "Last_Name" into a subfolder that is a hardcoded subfolder.

I receive an error that the "requested member of the collection does not exist".

I know this error occurs when you try to access an object that does not exist. The data base field is First_Name, and I have tried First Name as well in case the code was searching for the Address Field of First Name, which has been paired with the Data Base Field of First_Name. Here is what I have tried:

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:\folder\subfolder\subsubfolder\"


 ActiveDocument.SaveAs2 FileName:= _
        "C:\folder\subfolder\subsubfolder\" & firstname & lastname & ".docx"
End Sub

When I hardcoded the name with

firstname = "John"
lastname = "Doe"

I had no other errors and the active file saved.

I also attempted to use without success:

Dim firstname As Field
Dim lastname As Field
Was it helpful?

Solution

Instead of

firstname = ActiveDocument.FormFields("First_Name").Result

lastname = ActiveDocument.FormFields("Last_Name").Result

you need

firstname = ActiveDocument.MailMerge.DataSource.DataFields("First_Name").Value

lastname = ActiveDocument.MailMerge.DataSource.DataFields("Last_Name").Value

The name in quotes is case-sensitive (unusual in Windows programming). It has to match the name that Word is actually using, which may not be identical to the one in the data source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top