Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-30-2024, 06:36 AM
Chris_010101's Avatar
Chris_010101 Chris_010101 is offline Mail Merge Macro - Saves to PDF/Word Windows 11 Mail Merge Macro - Saves to PDF/Word Office 2021
Novice
Mail Merge Macro - Saves to PDF/Word
 
Join Date: Sep 2022
Posts: 4
Chris_010101 is on a distinguished road
Default Mail Merge Macro - Saves to PDF/Word

Hello

I use a macro to save mail merged documents into word and PDF files; very handy.


I must select all the rows above the row I need in the recipient list for it to work. If I only ticked "Sam" and left "Connor" and "Vincent" unticked, the macro will produce a "runtime error 5631" on

Code:
.Execute Pause:=False
Visa versa, if I ticked "Connor" and left "Sam" unticked, as long as "Vincent" is ticked, the macro will work.

This is unmanageable with 1000's of rows. The macro takes ages to run and I then have to delete all the documents I don't need after.

MS Forum Pic.png

Is it possible to fix the below so it will run on any row/rows I have selected in the recipient list, regardless of if the leading rows are selected?



Code:
Sub Split_2_PDF()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
                SelectedPath = vrtSelectedItem
            Next vrtSelectedItem
        Else
            MsgBox ("No Directory Selected.  Exiting")
            Exit Sub
        End If
    End With
    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 = .DataFields("File_Name")
            End With
            .Execute Pause:=False
            Application.ScreenUpdating = False
        End With
        
        'export pdf
        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
        ActiveDocument.Saved = True
        
        'export word
        ActiveDocument.SaveAs2 FileName:=docname & ".docx"
        ActiveDocument.Saved = True
        
        ActiveDocument.ActiveWindow.Close savechanges:=wdDoNotSaveChanges
        Documents(MainDoc).Activate
   Next i
    Application.ScreenUpdating = True
 
End Sub
Thanks
Reply With Quote
 

Tags
macro, mail merge, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Macro buttons to run PDF mail merge for a singular data entry with preset Word document JA6 Excel Programming 0 04-10-2024 07:32 AM
Mail Merge Macro - Saves to PDF/Word Creating a macro from a non-saved Word doc that duplicates the current open document and saves it mike0215 Word VBA 3 11-17-2017 01:40 PM
Mail Merge Macro - Saves to PDF/Word Mail Merge Macro ch1325 Word VBA 2 06-08-2015 06:18 AM
Mail Merge Macro spc94 Word VBA 2 06-04-2015 07:06 AM
Mail Merge Macro - Saves to PDF/Word Word Doc Macro (mail Merge) ajolson1964 Word VBA 1 05-10-2011 10:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:50 PM.


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