A problem than can occur when working with PDFCreator via code is that sometimes the code releases PDFCreator too early, and it isn't properly closed.
What is happening?
At the end of the PDFCreator routines on this site, we release PDFCreator with (a variation of) the following line:
Set pdfjob = Nothing
As long as the PDFCreator.exe task is (still) running, we'll get the error "Can't initialize PDFCreator." every time we try to run the PDF creation code:
So if you're seeing the above error, the issue is that a PDFCreator process is running on your system. If you opened PDFCreator manually, you may not have closed it. Or, if you had previously run a PDFCreator routine, then it had not finished doing what it needed to do, so the process that was actually running on the system stayed alive. We can check this by going into the Task Manager, (right click the taskbar and choose Task Manager,) and verifying that a PDFCreator.exe task is running.
Killing the running process:
The very first thing that we need to do to enable our code to run again is to close the running instance of PDFCreator. The steps to do this are listed below, but you should take great care to follow them exactly. Killing the wrong process could cause damage to your system, so make sure that you have the correct process selected! (You may want to print this, as the Task Manager will sit on top of all of your open windows.)
- Right click your taskbar
- Select Task Manager
- Click on the Processes tab
- Click Image Name until your list is sorted in alphabetical order
- Scroll down the list until you find PDFCreator.exe
- Right click PDFCreator.exe and choose End Process
- Once the process disappears, close the Task Manager
The first thing you should check about your code:
On March 1st, 2007, I updated all the PDFCreator articles on the site to change the way PDFCreator was released. Check your code to see if you have a loop near the end that looks something like this:
'Wait until the PDFCreator queue is clear Do Until pdfjob.cCountOfPrintjobs = 0 DoEvents Loop
If you do, try changing it to the following:
'Wait until the PDF file shows up then release the objects Do Until Dir(sPDFPath & sPDFName) <> "" DoEvents Loop
The reason that this works, is that instead of checking to see when the file shows up in the destination directory, the second variation checks to see when the PDFCreator queue is clear. At that point you can properly release the objects and they will close properly.
This article was originally published with the steps listed below to pause the PDFCreator code as I didn't have a better method at the time. Rather than remove them outright, I decided to leave them as an alternate way to work around the problem of closing PDFCreator. The method that follows is anything but elegant, but it is effective.
Alternative One - No time to adjust the code:
If you don't have time sleuth your your code, you could try closing all extra running programs to give your RAM and processor as much resources as possible. Often that is enough to allow a PDFCreator loop to complete.
The problems with this, obviously, are:
- We shouldn't need to close down our other applications to do this
- It won't always work
- It's not reliable when deploying code to clients. (Their systems will have different resource loads.)
Alternative Two - Coding a pause:
We can deal with this issue by intentionally slowing down our code with a pause. Personally, this goes against my grain, and is the reason I kept pursuing a better way. I want my code to complete as fast as possible, but if you don't mind this method, it will work.
The actual length of the pause that is required will depend on your system, your RAM, your processor, and how many applications you have open when the PDFCreator code is running. It may take some experimentation to get it correct, and you should be warned that it may not work on your client's PC. You may need to increase the pause there to be safe.
The following line of code just will add a 3 second pause in the routine. To adjust this pause, just increase or decrease the 3 to as much or little as you need.
Application.Wait Now + TimeValue("0:0:3")
The code above should be inserted just before the PDF job is closed. I.e. In the case of the (pre-updated) Printing Worksheets To A PDF File article, each of the routines would end:
'Wait until the PDF file shows up then release the objects Do Until Dir(sPDFPath & sPDFName) <> "" DoEvents Loop 'Wait a bit longer for PDF Creator to finish Application.Wait Now + TimeValue("0:0:3") pdfjob.cClose Set pdfjob = Nothing