Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro Won't Wait for PDF Conversion to Finish

Status
Not open for further replies.

rhpen

Programmer
Nov 2, 2005
40
US
Word XP, Acrobat 7.08

Am trying to convert the active Word document to pdf in my macro. After conversion I want to copy the pdf that was made to a different directory. The pdf maker window pops up and it starts to convert. The progress bar stops part way through and a vba error pops up stating "file not found". When I click "debug" it is always on the filecopy line. "Background=false doesn't appear to do any good. I also tried putting a for/next loop of varying lengths before the filecopy line to delay with no success. I can't seem to get the macro to wait til the pdf conversion is done. If I end macro execution, the pdf conversion then finishes and the file test.pdf is in the c:\temp directory, but of course the file hasn't been copied. (note: I set the pdfmaker filename to "test" and the output directory to "c:\temp" in the printer settings.)

Thank You


ActiveDocument.SaveAs FileName:="c:\test\test.doc", _
FileFormat:=wdFormatDocument
ActivePrinter = "Adobe PDF"
ActiveDocument.PrintOut Background = False
ActiveDocument.Close
FileCopy "c:\temp\test.pdf", "c:\data\test1.pdf"

 


How bout a loop
Code:
ActiveDocument.SaveAs FileName:="c:\test\test.doc", _
   FileFormat:=wdFormatDocument
ActivePrinter = "Adobe PDF"
ActiveDocument.PrintOut Background = False
ActiveDocument.Close

on error resume next
do
  err.clear
  FileCopy "c:\temp\test.pdf", "c:\data\test1.pdf"
until (err.number=0)
on error goto 0


Skip,

[glasses] [red][/red]
[tongue]
 
Thanks for the reply, Skip.

It just remained stuck in the loop forever with the PDF Maker popup window progress bar stuck part way through. When I broke the macro and went into debug mode, the PDF conversion completed.

RH,
 

Code:
ActiveDocument.SaveAs FileName:="c:\test\test.doc", _
   FileFormat:=wdFormatDocument
ActivePrinter = "Adobe PDF"
ActiveDocument.PrintOut Background = False
ActiveDocument.Close

on error resume next
do
  err.clear
  FileCopy "c:\temp\test.pdf", "c:\data\test1.pdf"[b]
  EnableEvents[/b]
until (err.number=0)
on error goto 0

Skip,

[glasses] [red][/red]
[tongue]
 



Code:
ActiveDocument.SaveAs FileName:="c:\test\test.doc", _
   FileFormat:=wdFormatDocument
ActivePrinter = "Adobe PDF"
ActiveDocument.PrintOut Background = False
ActiveDocument.Close

on error resume next
do
  err.clear
  FileCopy "c:\temp\test.pdf", "c:\data\test1.pdf"[b]
  EnableEvents
Loop[/b] until (err.number=0)
on error goto 0

Skip,

[glasses] [red][/red]
[tongue]
 
Thanks again Skip,

Unfortunately, I guess there is no EnableEvents in Word. I tried fudging one as in an article I found, but it still just seems to sit partially completed until I break the macro and go to debug mode (or end it) at which point the pdf conversion finishes. Article follows:

Sadly, Word does not have an Application.EnableEvents property.

However, you can dummy one up yourself. To do so, create a global variable named, for example, gbEnableEvents as a Boolean.

In the event handler set gbEnableEvents to False, let your code run, then re-set gbEnableEvents to True.

Elsewhere, put something like If gbEnableEvents Then
'your code here
Endif

Where necessary, you may need to store the value of the global variable, set it to false and reinstate its value at the end of a procedure.

Hope this helps.

Shauna Kelly. Microsoft MVP.
 
Try to replace this:
EnableEvents
with this:
DoEvents

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PH,

Tried that with same result. I have gotten around this by letting the macro run without copying (the PDFs get produced without trouble), then wrote a VB program to do the file copying separately when out of the Word macro. Not very elegant, but it gets the job done. Still, it would nice to be able to do it within the Word macro. It is odd that all the methods of getting the macro to wait until a process completes (that usually work fine) do not seem to work with printing to PDF Maker
 
Had this same problem with creating/copying a pdf. Had to tell the program to wait for so many seconds then copy the pdf. Now I might try using script to see if file exits if so copy if not wait and loop until it finds the file.
 
Thanks for the reply Bubba,

I tried your solution of looping until the file showed up. I would think it should work, but the pdf file never shows up. The PDF Maker gets stuck part way through its conversion and the macro just sits in the loop forever. The PDF Maker will not finish until I break the macro. It doesn't make any sense to me. I can put a STOP statement right after the line that makes the PDF and it will stop, but I cannot get it to wait correctly.
 
Don't know if this would work in word vba and whether you would find acceptable since user has to click ok for each report somewhat defeating the automation, but what I have done in MS-Access (until I try the timer suggestion) is to create a message box. Once the report finishes, a message box pops up and clicking on the ok button seems so far to allow the program to continue without going to never-never land. I'm using Access 2003 and Adobe Acrobat v7 pro.

You would place the message box on the line immediately following the code that sends the print to PDF.

Code:
    MsgBox "Finished Printing Report.  Click OK to print next report.", vbOKOnly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top