Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2018, 01:30 PM
meghanquint4 meghanquint4 is offline Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Windows 7 64bit Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Office 2010 64bit
Novice
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name
 
Join Date: Feb 2018
Posts: 2
meghanquint4 is on a distinguished road
Question Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name

The following macro works perfectly but I need to add how to save the file name as a specific Mail Merge field in the document.



Here is the query:
Code:
Option Explicit
Sub SaveAsSeparatePDFs()
'---------------------------------------------------------------------------------------------------
'---Script: SaveAsSeparatePDFs----------------------------------------------------------------------
'---Description: This subroutine saves MS Word document pages as separate PDFs with file names------
'----------------formatted like Page_x.pdf.---------------------------------------------------------
'---------------------------------------------------------------------------------------------------
 
Dim strDirectory As String, strTemp As String
Dim ipgStart As Integer, ipgEnd As Integer
Dim iPDFnum As Integer, i As Integer
Dim vMsg As Variant, bError As Boolean
 
1:
strDirectory = InputBox("Directory to save individual PDFs? " & _
vbNewLine & "(ex: C:\Users\Public)")
If strDirectory = "" Then Exit Sub
If Dir(strDirectory, vbDirectory) = "" Then
  vMsg = MsgBox("Please enter a valid directory.", vbOKCancel, "Invalid Directory")
  If vMsg = 1 Then
    GoTo 1
  Else
    Exit Sub
  End If
End If
2:
strTemp = InputBox("Begin saving PDFs starting with page __? " & _
  vbNewLine & "(ex: 32)")
bError = bErrorF(strTemp)
If bError = True Then GoTo 2
ipgStart = CInt(strTemp)
3:
strTemp = InputBox("Save PDFs until page __?" & vbNewLine & "(ex: 37)")
bError = bErrorF(strTemp)
If bError = True Then GoTo 3
ipgEnd = CInt(strTemp)
 
iPDFnum = ipgStart
On Error GoTo 4:
For i = ipgStart To ipgEnd
  ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    strDirectory & "\Page_" & iPDFnum & ".pdf", ExportFormat:=wdExportFormatPDF, _
    OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
    wdExportFromTo, From:=i, To:=i, Item:=wdExportDocumentContent, _
    IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
    wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=False, UseISO19005_1:=False
    iPDFnum = iPDFnum + 1
Next i
End
4:
vMsg = MsgBox("Unknown error encountered while creating PDFs." & vbNewLine & vbNewLine & _
  "Aborting", vbCritical, "Error Encountered")
End Sub

Private Function bErrorF(strTemp As String) As Boolean
Dim i As Integer, vMsg As Variant
bErrorF = False
If strTemp = "" Then
  End
ElseIf IsNumeric(strTemp) = True Then
  i = CInt(strTemp)
  If i > ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) Or i <= 0 Then
    Call msgS(bErrorF)
  End If
Else
  Call msgS(bErrorF)
End If
End Function

Private Sub msgS(bMsg As Boolean)
Dim vMsg As Variant
vMsg = MsgBox("Please enter a valid integer." & vbNewLine & vbNewLine & _
  "Integer must be > 0 and < total pages in the document (" & _
  ActiveDocument.BuiltInDocumentProperties(wdPropertyPages) & ")", vbOKCancel, "Invalid Integer")
If vMsg = 1 Then
  bMsg = True
Else
  End
End If
End Sub
At the top of my Word Doc, there is a mail merge field from an Excel Doc with a specific Clinic name. I need help figuring out how to save the PDFs as the following: "2018_Clinic_Level_Update_1_Clinic name.pdf" without a page number. The clinic name would differentiate the file names.

What should be changed in the above macro to make this happen??

Please help as soon as possible!!

Last edited by macropod; 02-27-2018 at 05:38 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 02-27-2018, 05:45 PM
macropod's Avatar
macropod macropod is offline Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Windows 7 64bit Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can't retrieve a mergefield name from the output document. In any event, it would be more efficient to generate the individual output files as part of the merge process rather than doing it afterwards. For code to do that (and name the output files according to field names in the data source), see Send Mailmerge Output to Individual Files in the Mailmerge Tips and Tricks 'Sticky' thread at the top of the mailmerge forum:
https://www.msofficeforums.com/mail-...ps-tricks.html

If you're wedded to the existing process, see Split Merged Output to Separate Documents in the same thread for pointers on how you might retrieve the desired filenames.

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-28-2018, 07:12 AM
meghanquint4 meghanquint4 is offline Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Windows 7 64bit Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Office 2010 64bit
Novice
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name
 
Join Date: Feb 2018
Posts: 2
meghanquint4 is on a distinguished road
Default

Thank you so much! The Send Mailmerge Output to Individual Files thread works perfectly! I was stuck on this for about 5 hours yesterday until I decided to post a thread. This helps me out a lot. Thank you again Macropad!
Reply With Quote
Reply

Tags
macro edit, mail merge fields, pdf save error

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Tweak Macro to Save Each Page of Word Document as Separate PDF or Doc File? Hewg74 Word VBA 3 08-22-2016 05:20 PM
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Merge separate words files into one master file. ronka1997 Word 3 08-09-2016 01:26 AM
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Mail merge result in separate world files instead of one file containing all the data xdhbsh Mail Merge 3 12-24-2015 12:23 AM
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Creating separate file for each mail merge doshshirl Mail Merge 3 02-15-2014 08:49 PM
Microsoft Word VBA to save Word doc to separate PDF files using a Mail Merge field as File name Word Mail Merge File Save mickeyw3340 Mail Merge 2 12-18-2012 11:30 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft