I have a macro that runs a mail merge with Word, creates an invoice for each client, and saves it as a PDF with each client's name appended to the end of the file name.
I'm getting a run time error 5941 "the requested member of the collection does not exist" at the line of code that names the file (highlighted in red).
I'm not a VBA expert and need to accomplish this task this morning so any help is appreciated. Thanks.
[/CODE]
I'm getting a run time error 5941 "the requested member of the collection does not exist" at the line of code that names the file (highlighted in red).
I'm not a VBA expert and need to accomplish this task this morning so any help is appreciated. Thanks.
VBA Code:
Sub Merge_to_pdf() '
' merge1record_at_a_time Macro
'
'
Dim fd As FileDialog
'Create a FileDialog object as a Folder Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
'Use the Show method to display the Folder Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
SelectedPath = vrtSelectedItem
Next vrtSelectedItem
Else
MsgBox ("No Directory Selected. Exiting")
Exit Sub
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
Application.ScreenUpdating = False
MainDoc = ActiveDocument.Name
ChangeFileOpenDirectory SelectedPath
For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
[COLOR=rgb(184, 49, 47)][B]DocName = "Test Name - " & .DataFields("First_Name").Value & " " & .DataFields("Last_Name").Value & ".pdf" ' ADDED CODE[/B][/COLOR]
End With
.Execute Pause:=False
Application.ScreenUpdating = False
End With
ActiveDocument.ExportAsFixedFormat OutputFileName:=DocName, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveWindow.Close SaveChanges:=False
Next i
Application.ScreenUpdating = True
End Sub
[CODE=vba]