|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How can I save a Word Document as a PDF file with a merged field filename?
Hi,
I have a document which I constantly change with the Mail Merge tool. I would like to create a macro that saves the document as a PDF file, but changes the filename to the merge field data in the document. How can I do this? I appreciate any help I could receive. Thank you! |
#2
|
||||
|
||||
If you bookmark the field you want to use for the filename with the name 'Name', you could use a macro like the following. Simply preview the record to be output, then run the macro. The output file is saved to the same folder as the mailmerge main document.
Code:
Sub Demo() Dim StrPath As String, StrName As String With ActiveDocument StrPath = .Path & "\" StrName = .Bookmarks("Name").Range.Text .SaveAs2 FileName:=StrPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 08-08-2014 at 04:39 PM. Reason: Simplified macro |
#3
|
|||
|
|||
It worked perfectly! Just one thing, is there any way I can also print the document automatically with the same macro?
|
#4
|
||||
|
||||
Simply insert:
.Printout before: End With Note: this will print the document, not the PDF, but that shouldn't matter.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi, I get an "Compile error: Expected: end of statement" error on the save line.
dt = Format(CStr(Now), "mmddyyy") ChangeFileOpenDirectory "\\vcuhshmo\groups\Claims Administration\CCU\PVOH\PVOH Mail Merge Letters\PVOH Mailers - Test\" With ActiveDocument ' StrPath = .path & "\" StrName = .Bookmarks("Name").Range.Text .SaveAs FileName:=StrName & ".docx" End With What have I missed? |
#6
|
||||
|
||||
It looks like your code has merged two lines although that may be how you pasted it. Try
Code:
ChangeFileOpenDirectory "\\vcuhshmo\groups\Claims Administration\CCU\PVOH\PVOH Mail Merge Letters\PVOH Mailers - Test\" With ActiveDocument StrName = .Bookmarks("Name").Range.Text .SaveAs FileName:=StrName & ".docx" End With Done properly, there is also no need for ChangeFileOpenDirectory! Code:
Dim StrFldr as String StrFldr = "\\vcuhshmo\groups\Claims Administration\CCU\PVOH\PVOH Mail Merge Letters\PVOH Mailers - Test\" With ActiveDocument StrName = .Bookmarks("Name").Range.Text .SaveAs FileName:=StrFldr & StrName & ".docx" End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
merge field, save-as, word 2010 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Save merged document as concatenated merge field values | texas791 | Word VBA | 4 | 02-25-2014 07:35 PM |
Macro to create new word doc and save the file using String found in the document | VBNation | Word VBA | 2 | 02-08-2013 07:14 AM |
Auto update Filename field | Oliver Beirne | Word VBA | 4 | 10-19-2012 03:33 AM |
Save Filename using Document Text | Knawl | Word | 11 | 10-10-2011 03:00 AM |
Word: The document 'Filename' caused a serious error the last time ... | martincruise | Word | 0 | 02-25-2010 01:47 AM |