Share via

How to fix this macro for Word

Rachel Lachiusa 10 Reputation points
2026-02-16T14:24:09.3233333+00:00

Hi! I have been using the same macro for 3 years and it suddenly stopped working. My purpose of this macro is to take a mail merge and create PDFs with customized file names. They imported straight to a network folder. I now receive error code 5825 no object found. I have tried saving to OneDrive instead of a Network folder and still no luck. Any help is appreciated.

Sub MailMergeToPdfBasic()                                                        ' Mark the start of the Subroutine (i.e. Macro) and name it "MailMergeToPdf"
' Macro created by Imnoss Ltd
' Please share freely while retaining attribution
' Last Updated 2021-05-03
    Dim masterDoc As Document, singleDoc As Document, lastRecordNum As Long   ' Create variables ("Post-it Notes") for later use
    Set masterDoc = ActiveDocument                                               ' Identify the ActiveDocument (foremost doc when Macro run) as "masterDoc"

    masterDoc.MailMerge.DataSource.ActiveRecord = wdLastRecord                   ' jump to the last active record (active = ticked in edit recipients)
    lastRecordNum = masterDoc.MailMerge.DataSource.ActiveRecord                  ' retrieve the record number of the last active record so we know when to stop

    masterDoc.MailMerge.DataSource.ActiveRecord = wdFirstRecord                  ' jump to the first active record (active = ticked in edit recipients)
    Do While lastRecordNum > 0                                                   ' create a loop, lastRecordNum is used to end the loop by setting to zero (see below)
        masterDoc.MailMerge.Destination = wdSendToNewDocument                    ' Identify that we are creating a word docx (and no e.g. an email)
        masterDoc.MailMerge.DataSource.FirstRecord = masterDoc.MailMerge.DataSource.ActiveRecord              ' Limit the selection to just one document by setting the start ...
        masterDoc.MailMerge.DataSource.LastRecord = masterDoc.MailMerge.DataSource.ActiveRecord               ' ... and end points to the active record
        masterDoc.MailMerge.Execute False                                        ' run the MailMerge based on the above settings (i.e. for one record)
        Set singleDoc = ActiveDocument                                           ' Identify the ActiveDocument (foremost doc after running the MailMerge) as "singleDoc"
        singleDoc.SaveAs2 _
            FileName:=masterDoc.MailMerge.DataSource.DataFields("DocFolderPath").Value & Application.PathSeparator & _
                masterDoc.MailMerge.DataSource.DataFields("DocFileName").Value & ".docx", _
            FileFormat:=wdFormatXMLDocument                                      ' Save "singleDoc" as a word docx with the details provided in the DocFolderPath and DocFileName fields in the MailMerge data
        singleDoc.ExportAsFixedFormat _
            OutputFileName:=masterDoc.MailMerge.DataSource.DataFields("PdfFolderPath").Value & Application.PathSeparator & _
                masterDoc.MailMerge.DataSource.DataFields("PdfFileName").Value & ".pdf", _
            ExportFormat:=wdExportFormatPDF                                      ' Export "singleDoc" as a PDF with the details provided in the PdfFolderPath and PdfFileName fields in the MailMerge data
        singleDoc.Close False                                                    ' Close "singleDoc", the variable "singleDoc" can now be used for the next record when created
        If masterDoc.MailMerge.DataSource.ActiveRecord >= lastRecordNum Then     ' test if we have just created a document for the last record
            lastRecordNum = 0                                                    ' if so we set lastRecordNum to zero to indicate that the loop should end
        Else
            masterDoc.MailMerge.DataSource.ActiveRecord = wdNextRecord           ' otherwise go to the next active record
        End If

    Loop                                                                         ' loop back to the Do start
End Sub                                                                          ' Mark the end of the Subroutine


Mod moved from : Microsoft 365 and Office | Word | For home | Windows

Developer technologies | Visual Basic for Applications

2 answers

Sort by: Most helpful
  1. John Korchok 231K Reputation points Volunteer Moderator
    2026-03-19T23:52:58.95+00:00

    If you get an error while trying to close a file, it may be because there isn't currently a file open. The previous lines save the document as .docx, then exports it to PDF. Are those files being created? It is unreliable to repeatedly use the Set command along with ActiveDocument, because it's easy to accidentally have the wrong document being the active one, or for there to be no document open. This may be why the macro works some days and not others.

    FWIW, the error line has a problem. Here is the reference page for Document.Close. The only parameter that takes a False argument is RouteDocument. If that's the intended parameter, you would rephrase

    singleDoc.Close False
    

    as

    singleDoc.Close RouteDocument:=False
    

  2. Q&A Assist
    2026-02-16T14:24:25.0666667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The error code 5825, indicating "no object found," typically occurs when the macro is trying to access an object that doesn't exist or is not available. Here are some steps to troubleshoot and possibly fix your macro:

    1. Check Data Source: Ensure that the mail merge data source is correctly set up and that the fields DocFolderPath, DocFileName, PdfFolderPath, and PdfFileName are present and correctly populated in your data source. If any of these fields are missing or have incorrect values, it could lead to the error.
    2. Active Document: Make sure that the ActiveDocument is indeed the master document intended for the mail merge. If another document is active when the macro runs, it may not find the expected objects.
    3. Record Availability: Verify that there are active records in your mail merge. If there are no records selected, the macro will not find any objects to process, leading to the error.
    4. File Paths: Ensure that the paths provided in the DocFolderPath and PdfFolderPath fields are valid and accessible. If the paths point to a network location, ensure that you have the necessary permissions to write to that location.
    5. Debugging: You can add debugging statements (e.g., Debug.Print) to output the values of critical variables before they are used, which can help identify where the issue lies.
    6. Update References: If you have recently updated Word or your operating system, check if there are any changes in how Word handles macros or file paths that might affect your code.
    7. Test with Simplified Code: Try running a simplified version of your macro that only processes one record to see if the issue persists. This can help isolate the problem.

    If after these checks the issue remains unresolved, consider running the macro in a different environment or reinstalling Microsoft Office to ensure there are no underlying issues with the application itself.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.