Solved

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

Posted on 2011-10-01
8
164 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

786 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