Solved

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

Posted on 2011-10-01
8
161 Views
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.
0
Comment
Question by:mycomac
  • 3
  • 2
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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
    ActiveDocument.Close
End Sub

Open in new window

0
 

Author Comment

by:mycomac
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.
0
 

Author Comment

by:mycomac
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 76

Expert Comment

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

Accepted Solution

by:
GrahamSkan earned 500 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.

0
 
LVL 100

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now