View Single Post
 
Old 02-15-2018, 11:35 AM
Btop Btop is offline Windows 10 Office 2016
Novice
 
Join Date: Feb 2018
Posts: 17
Btop is on a distinguished road
Default Compatibility of 2 macros in mail merge: Delete table rows + save individual PDFs

I'm writing a mail merge letter, but before I keep going, I'd need to know if the use of these two macros would work:

MACRO 1 (Deletes row, if first column = $)

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 2/12/2018
Dim oTbl As Table
Dim lngIndex As Long
  For Each oTbl In ActiveDocument.Tables
    For lngIndex = oTbl.Rows.Count To 1 Step -1
       If Left(oTbl.Cell(lngIndex, 1).Range.Text, Len(oTbl.Cell(lngIndex, 1).Range.Text) - 2) = "$" Then
         oTbl.Rows(lngIndex).Delete
       End If
    Next
  Next
lbl_Exit:
  Exit Sub
End Sub
MACRO 2 (Saves all mail merge documents as individual PDF files)

Code:
Sub merge1record_at_a_time() '
' merge1record_at_a_time Macro
'
'
    Dim fd As FileDialog


    'Create a FileDialog object as a Folder Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd


        'Use the Show method to display the Folder Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then
                For Each vrtSelectedItem In .SelectedItems


                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
        SelectedPath = vrtSelectedItem


        Next vrtSelectedItem


        Else
        MsgBox ("No Directory Selected.  Exiting")
        Exit Sub
        End If
    End With


    'Set the object variable to Nothing.
    Set fd = Nothing


Application.ScreenUpdating = False


MainDoc = ActiveDocument.Name
    ChangeFileOpenDirectory SelectedPath
    For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = i
                .LastRecord = i
                .ActiveRecord = i
                docName = "Letter1 - " & .DataFields("Contact1").Value & ".pdf"      ' ADDED CODE
            End With
            .Execute Pause:=False
    Application.ScreenUpdating = False
            
        End With
    ActiveDocument.ExportAsFixedFormat OutputFileName:=docName, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
    ActiveWindow.Close SaveChanges:=False

    Windows(MainDoc).Activate
    Next i
Application.ScreenUpdating = True


End Sub
Both of these macros work perfectly on their own, but I'd need MACRO 1 to apply to the letters, before these are saved as PDFs with MACRO 2. I believe MACRO 1 needs to be run after finalizing the mail merge, whilst I've only used MACRO 2 before finalizing the mail merge.
Reply With Quote