Solved

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

Posted on 2011-10-01
8
158 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

19 Experts available now in Live!

Get 1:1 Help Now