Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How to automatically save & name a document as a PDF when closing.

Posted on 2011-10-01
Medium Priority
Last Modified: 2012-05-12
I have a word template that generates a contract using data from a non-microsoft windows app.

I want to use a VBA script that will save the file as a PDF (using Acrobat or CutePDF Writer) to a path, and with a filename that will be determined from merge fields in the merged document.
Question by:mycomac
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 76

Expert Comment

ID: 36900074
That would normally be done by putting some code in the Document_Close event in the ThisDocument module, but there are a number of problems.

Firstly, in which Template project would it go? A mail merge Main document is not technically a template in the Word sense. Therefore result documents do not reference it, so the event wouldn't be fired.

If the code were in the main document's own template (probably the Normal template), then the code would have to identify the document as one that has to be processed in this way, and not one of the many othere documents that you might want to open or create and close.

Another problem is that a mailmerge execution  produces an output for each record processed. If it is another documents, each record's result is one section of the document, so the pdf would have the name of one of the records only.

Finally, there could be a problem in location the text that replaces the mailmerge field in the main document.

However here is a simple example

Private Sub Document_Close()
    If Trim$(ActiveDocument.words(1)) = "My Title" Then
        ActiveDocument.SaveAs "C:\MyFolder\" & Trim$(ActiveDocument.words(2)) & ".pdf", wdFormatPDF
    End If
End Sub

Open in new window


Author Comment

ID: 36901236
Thanks for your thoughtful reply.

The "template" is currently saved as a .doc, not a .dot - if that matters.

Each contract is generated independently from a single database record, never multiple contracts in a batch.

I'll give the code a try and see what happens.

Author Comment

ID: 36901246
If I put this into the "template's" ThisDocument, it breaks at ActiveDocument.Close

Is there a way to pass this script on to the product of the templates output so it will only run then?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 76

Expert Comment

ID: 36901950
Do you men that there is an error? If so, what is the error message?
LVL 76

Accepted Solution

GrahamSkan earned 2000 total points
ID: 36902384
Oops. I didn't test it properly last night. The line gives a "Command Failed" error. Here is an improved version
Private Sub Document_Close()
    If Trim$(ActiveDocument.words(1)) = "MyTitle" Then
        ActiveDocument.SaveAs "C:\MyFolder\" & Trim$(ActiveDocument.words(2)) & ".pdf", wdFormatPDF
        ActiveDocument.Saved = True
    End If
End Sub

Open in new window

It might be possible to devise some code to copy the macro to the document, but it can be tricky.

If you only need the functionality on the system that does the merge, the advantage would be to save the need to identify the document, but not much more.

If you want it to run on a limited number of systems, you could put the macro in a template and put it in the Word startup folder on the target systems.

If you want to be able to send it more universally, let us know and we'll see if the trickiness can be overcome.

LVL 101

Expert Comment

ID: 37144352
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
In a previous video Micro Tutorial here at Experts Exchange (, I explained how to get a free, one-month trial of Office …
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question