VBA & Macro Problems

Fix a Word VBA Macro That Uses the Wrong Document Filename

Camila MilosovichCamila Milosovich Nov 14, 2026 868 views

Question details

The user needs a Word VBA macro to append text to the current document's filename and export it as a PDF.

Fix a Word VBA Macro That Uses the Wrong Document Filename
Product
Microsoft Word
Device & OS
not provided
Scenario
A macro stored in a Word template is used to create a PDF named after the active document.
Observed behavior
The macro uses Normal.dotm as the filename instead of the current document's name.
Before you start

Open the document you want to export and save it using File > Save As if it does not yet have a location. Make sure the document is active before running the macro.

Solution 1Recommended

Use ActiveDocument to Name the PDF

Use this approach when the macro is stored in Normal.dotm or another template but should export the currently active document.

ThisDocument refers to the document or template that contains the macro. ActiveDocument refers to the document currently active in Word.

The example removes the document's final extension, adds a suffix, and saves the PDF in the same folder as the document.

1
Open the macro editor

In Word, open Developer > Visual Basic, then find the macro that creates the PDF.

2
Replace the document reference

Replace ThisDocument.FullName with ActiveDocument.FullName where the macro needs the active document's full path. To work with just its filename, use ActiveDocument.Name.

3
Build and export the PDF name

Alternatively, adapt this complete macro in the Visual Basic editor: Sub ExportActiveDocumentPDF() Dim doc As Document Dim baseName As String Dim pdfPath As String Dim dotPos As Long Set doc = ActiveDocument If doc.Path = "" Then MsgBox "Save the document before exporting it." Exit Sub End If baseName = doc.Name dotPos = InStrRev(baseName, ".") If dotPos > 0 Then baseName = Left(baseName, dotPos - 1) pdfPath = doc.Path & Application.PathSeparator & baseName & "_suffix.pdf" doc.ExportAsFixedFormat OutputFileName:=pdfPath, ExportFormat:=wdExportFormatPDF End Sub

4
Run the macro

Return to the document, select it as the active window, and run the macro from View > Macros. Check the document's folder for the PDF with the added suffix.

Use ActiveDocument to Name the PDF
Choose your suffix: Replace _suffix in the example with the text you want to append. Check the destination folder for an existing PDF with the same name before exporting.
Free Microsoft Office alternative

Export Documents to PDF with WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface for opening documents and exporting them to PDF. It supports common Word formats for an easier move, but it does not fix a Microsoft Word VBA macro.

Export a document to PDF without relying on a Word VBA macroOpen and edit Microsoft Word DOC and DOCX filesUse a familiar interface that makes migration easierWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does ThisDocument return Normal.dotm?

ThisDocument refers to the document or template containing the macro. If the macro is stored in Normal.dotm, ThisDocument.FullName points to that template.

How do I get the active document's full path?

Use ActiveDocument.FullName in the Word VBA editor. Use ActiveDocument.Name for the filename alone or ActiveDocument.Path for its folder.

What if the document has not been saved yet?

Use File > Save As to give the document a location before running a macro that saves the PDF in the document's folder. The example checks for an empty document path and stops if necessary.

How can I change the text added to the PDF filename?

In the example macro, replace _suffix in the pdfPath assignment with your preferred text. Keep the .pdf extension at the end.