About a year ago I used code like the one below to create individual pdf files from a merged Word document that had a unique file name for each of the output pdf files based on a field in the data file.
Can't get it to work. Any guidance would be appreciated.
Sub Merge_To_Individual_Files()
Application.ScreenUpdating = False
Dim strFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Set MainDoc = ActiveDocument
With MainDoc
strFolder = .Path & Application.PathSeparator
For i = 1 To .MailMerge.DataSource.RecordCount
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
If Trim(.DataFields("File_Name")) = "" Then Exit For
StrName = .DataFields("File_Name")
End With
.Execute Pause:=False
End With
For j = 1 To 255
Select Case j
Case 1 To 31, 33, 34, 37, 42, 44, 46, 47, 58 - 63, 91 - 93, 96, 124, 147, 148
StrName = Replace(StrName, Chr(j), "")
End Select
Next
StrName = Trim(StrName)
With ActiveDocument
'.SaveAs FileName:=\\houhome\~oconnod\My Documents\Test & Test 2 5 One Plan BUIP Template & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False '
.Close SaveChanges:=False
End With
Next i
End With
Application.ScreenUpdating = True
End Sub
Can't get it to work. Any guidance would be appreciated.
Sub Merge_To_Individual_Files()
Application.ScreenUpdating = False
Dim strFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Set MainDoc = ActiveDocument
With MainDoc
strFolder = .Path & Application.PathSeparator
For i = 1 To .MailMerge.DataSource.RecordCount
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
If Trim(.DataFields("File_Name")) = "" Then Exit For
StrName = .DataFields("File_Name")
End With
.Execute Pause:=False
End With
For j = 1 To 255
Select Case j
Case 1 To 31, 33, 34, 37, 42, 44, 46, 47, 58 - 63, 91 - 93, 96, 124, 147, 148
StrName = Replace(StrName, Chr(j), "")
End Select
Next
StrName = Trim(StrName)
With ActiveDocument
'.SaveAs FileName:=\\houhome\~oconnod\My Documents\Test & Test 2 5 One Plan BUIP Template & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False '
.Close SaveChanges:=False
End With
Next i
End With
Application.ScreenUpdating = True
End Sub