Thread: Macro Help!
View Single Post
 
Old 02-13-2017, 09:54 AM
greatlakes25 greatlakes25 is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2017
Posts: 1
greatlakes25 is on a distinguished road
Default Macro Help!

Hi, I currently have a macro in Microsoft Word to take a mail merge and save individual files as individual pdfs. As of right now, I choose the folder to save all the files in. I would like to modify the last part to save files to a specific folder already created based on the original excel document I used to create the mail merge. Any help would be greatly appreciated!!! I pasted my macro below.



Code:
Option Explicit


Sub Merge_to_pdf()
'merges one record at a time to the chosen output folder
Application.ScreenUpdating = False
Dim strFolder As String, StrName As String, MainDoc As Document, i As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set MainDoc = ActiveDocument
With MainDoc
  For i = 1 To .MailMerge.DataSource.RecordCount
    With .MailMerge
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        If Trim(.DataFields("Last_Name")) = "" Then Exit For
        StrName = .DataFields("Last_Name") & ", " & .DataFields("First_Name")
      End With
      .Execute Pause:=False
    End With
  With ActiveDocument
    .ExportAsFixedFormat OutputFileName:=strFolder & "" & StrName & ".pdf", ExportFormat:=wdExportFormatPDF, _
    OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
    Item:=wdExportDocumentContent, IncludeDocProps:=True, CreateBookmarks:=wdExportCreateNoBookmarks, _
    KeepIRM:=True, DocStructureTags:=True, BitmapMissingFonts:=True, UseISO19005_1:=False
    .Close SaveChanges:=False
  End With
Next i
End With
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Last edited by macropod; 02-13-2017 at 02:11 PM. Reason: Added code tags & formatting
Reply With Quote