Fix a Word VBA Macro That Uses the Wrong Document Filename
Question details
The user needs a Word VBA macro to append text to the current document's filename and export it as a PDF.

- 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.
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.
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.
In Word, open Developer > Visual Basic, then find the macro that creates the PDF.
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.
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
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.

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.

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.




