View Single Post
 
Old 02-27-2018, 01:30 PM
meghanquint4 meghanquint4 is offline Windows 7 64bit Office 2010 64bit
Novice
 
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