Word Merge to Individual PDF with file name from data file

oconnod

New Member
Joined
Feb 24, 2016
Messages
11
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
 
I'm not sure I'm running the macro at the right time. I have my Word document template with the inserted fields and my Excel file with the data. I select the preview in the Word document and then run the macro. Is that right?
Superficially, yes, but the error message indicates there is another macro somewhere that is failing to run to completion. There is also no need to Preview the merge before printing.
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi,

I'm able to get the macro to work but in the merged document the formatting is not displaying as it should. For example, in Excel the cell displays 1.36 but in the merged document it shows 1.3600000000000001.

Also, is it possible to use a table in the Word document without disrupting the macro process?

Thanks again for all your help!
 
Upvote 0
I think I got it. I'm able to edit the field and select Preserve formatting during updates and the chart does not seem to be causing any problems.

Thanks
 
Upvote 0
I'm able to get the macro to work but in the merged document the formatting is not displaying as it should. For example, in Excel the cell displays 1.36 but in the merged document it shows 1.3600000000000001.

Also, is it possible to use a table in the Word document without disrupting the macro process?
Neither of these issues has anything to do with the macro.

For your mergefield formatting issues, see the links in my first reply in this thread.

As for the table, you need to explain what your issues are.
 
Upvote 0
Me again.... I can't get the format from Excel to carryover to the Word document when merged. I have placed the various formats needed in the first 8 columns. Otherwise the merge is working fine.

Thanks
 
Upvote 0
Got it to work. I went down the wrong path for trying to get the formatting to work. Went back to your first post and it is obvious.....
I'll never forget how to do this again!

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top