PDA

View Full Version : Solved: Print Word doc directly as pdf



Bilby
11-08-2006, 05:14 PM
Greetings All,

My boss wants me to print a Word Document directly as a PDF from within Word. To clarify,
1. With a normal Word 2003 document active I intercept the Print events - done
2. VBA code in the custom print event then converts this doc into a PDF.
3. VBA code then uses Shell to print the PDF.

BUT

Trying to do all this within Word doesn't work becouse it can take up to 4 minutes to generate the PDF. I've tried using a Timer procedure to check the PDF directory for the resulting PDF before trying to print it but to no avail.

I can successfully create the PDF (with timer commented out), if I then comment the PDF creation out then I can successfully run the timer & print. But it appears I can't do 'em both.

Can anyone help me:banghead:

fumei
11-09-2006, 01:22 AM
While there certainly are some issues with it, get PDFMaker and then you can print PDF directly from Word. No VBA.

Bilby
11-09-2006, 01:39 PM
Unfortunately that may not do. The problem isn't printing a PDF from Word.
Its printing an active Word document, by generating a PDF and print THE PDF all at once. Its the PDF that must be printed and without the user doing anymore than 1 mouse click( customized 'Print' event will be used to launch this operation).

lucas
11-09-2006, 02:32 PM
I may be missing something but why does it have to be printed from the pdf? It would look the same if you print it from word. You can print one copy to the printer and one copy to pdf file......?

Bilby
11-09-2006, 07:42 PM
Yo Steve,

It does look strange doesn't it?
This copy of Word sits on a Document Management System Server with lotsa remote users with even more numerous printers (and more particularly Printer Drivers).
I don't really understand the reason but the guys that run the DMS HATE keeping up with printer drivers.
Hence the users Word document is to be converted into a PDF and the PDF is then printed to the users own printer. All this without the user having to do any extra steps.

The current play of my problem: I have a sortof solution. It relies on a Windows API Timer to force the program to wait until the PDF is completed before attempting to print it. Unfortunately I don't seem to be able to kill the timer as I have no way of getting a handle/ID on it.

If you've got any ideas on creating (and hopefully killing) Win API timers or any alternative ways of tackling my problem I'd really appreciate it. Especially interested on how you get a Windows handle (whatever that is) on my rebellious timer.

:SHOCKED: Bilby

lucas
11-09-2006, 09:42 PM
I don't know what kind of timer your running but you might try something like this:
Application.OnTime When:=Now + TimeValue("00:00:01"), _
Name:="check"
where "check" is the procedure to: check the PDF directory for the resulting PDF
this is set up for one second as is but is flexible

Bilby
11-13-2006, 01:59 PM
I never knew about the OnTime thanks.

I eventually got a work around by launching a VB6 form which has a timer and then prints the PDF. Do you know how to get the window handle using VBA as in the following example.

ShellExecute 0&, "Print", "C:\SOmething.exe", 0&, 0&, SW_SHOWNORMAL

where the first 0& is the windows handle?

fumei
11-13-2006, 06:15 PM
This is so strange....

Ken Puls
11-14-2006, 06:37 AM
I eventually got a work around by launching a VB6 form which has a timer and then prints the PDF. Do you know how to get the window handle using VBA as in the following example.

ShellExecute 0&, "Print", "C:\SOmething.exe", 0&, 0&, SW_SHOWNORMAL

where the first 0& is the windows handle?
I'm not 100% sure on this, but if I interpret Ivan's code correctly:

Public Declare Function GetDesktopWindow Lib "user32" () As Long

Sub Test
Dim hWndDesk As Long
'// use the desktop as default ... you should use your App.handle
hWndDesk = GetDesktopWindow()
End Sub
EDIT: The Sub/End Sub portion of the above is only to separate the parts you'd roll into your routine from the API call. If you were going to use this as I've shown above, it should be in a function.

Based on his note, though, you may want to try with the application's handle as well, which you can get from:
Application.Hwnd
Original source is Ivan's excellent writeup on ShellExecute here (http://www.xcelfiles.com/ShellExecuteA.html).


This is so strange.... Would be tempted to agree with you, Gerry. :)

fumei
11-14-2006, 07:11 AM
Yes, well for me it started with the comment about the DMS guys hating to update printer drivers.

Well...duh. Considering Word's relationship with printer drivers...there is likely an evil eye lurking within....waiting....

It just seems an extraordinary amount of stuff to put together for what? The poor wee users don't have to take any more steps? Boo-hoo. Why...does it take too long? Longer than the 4 minutes it takes to create the PDF and Shell out to print it to the user's printer?

Bilby, this is not a criticism of you. No doubt you are doing what the powers that be are telling you to do. We have all been THERE. However, the thing does seem strange to me.

Anyways, hope the API timer thing is working for you now. Good luck.

Ken Puls
11-14-2006, 05:30 PM
It just seems an extraordinary amount of stuff to put together for what? The poor wee users don't have to take any more steps? Boo-hoo. Why...does it take too long? Longer than the 4 minutes it takes to create the PDF and Shell out to print it to the user's printer?

:rotlaugh:

You know... I automate a LOT of things at work, and one day one of our managers calls me asking me to do something. I can't remember what it was, but it was going to be a really bad return on time. Something like a week to create something that would be used three times a year for an hour or something. Might be an exageration, but it was a bad use of time.

I told my boss, and he started shaking his head. "Not everyting HAS to be automated" he said. He's right, but try explaining THAT to a user! LOL!

Bilby
11-19-2006, 01:41 PM
Well people they pay me each forynight & sometimes thats all the reason thats required.
Thanks for your help, I've got the thing working.
:hi:

Switcha
12-20-2006, 04:04 AM
Bilby,
If you happen to work for the BIG "T", let me know, as I may need to do the same thing, but Print in PDF, to reduce the print queue time from DM, as I have hordes of coworkers ready to delet my prints. Hopefully PDF'ing will reduce the spooltime, etc. Would love to utilise your code.

Switcha

:beerchug:

:devil2: