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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I AUTOMATE a pdf file to open, print & close

Status
Not open for further replies.

birdpharmer

Technical User
Mar 12, 2007
4
US
I work in a pharmacy. Pharmacist are now required under Federal Law to hand out an ever increasing number of leaflets related to certain medications. For example, anyone who is dispensed an antidepressant should now be receiving a 3 page warning leaflet on antidepressants and suicide. To solve the problem of keeping a multitude of pages, I have scanned each warning leaflet into a PDF file. Using the MS Office tool bar, I have created a shortcut for each leaflet. But, You have to hit the shortcut, then print the leaflet, then close the shortcut. I know this sounds easy, but a Pharmacists life is measured in seconds while working. I would like some way to automate this task so that a simple click of one shortcut opens, prints, then closes. If I can save 30 seconds here, I have just that much more time for direct patient contact.

Any help here would be greatly appreciated.

Thanks
 
Hi,

Are you sure you want to open each PDF file in an application in order to print it? When I had a similar requirement I found it quicker just to right-click on the required PDF file and choose the 'Print' option from the context-menu.



 
Unfortunately adobe don't provide a command line interface. This makes automatic printing rather tricky - How are you at writing activeX controls?

However if your printer is connected to to an LPT port, or it is networked you can use a rather neat 'kludge' by using command mode (cmd) and a batch file.

Print the document in the normal way - but tick the 'print to file' box and give the output file a name - lets use "myoutput.out" for now as the example. You only need to do this once.

If the printer is attached to LPT1 then the dos command

copy /B myoutput.out lpt1:

will print the file to the printer on port LPT1 - the /B denotes a binary copy and is essential.

So if you create a batch file containing that command followed by exit it will do the job. Change your shortcut to point to that batch file. N.B. use the full path to the output file in the batch file - i.e. copy /B "C:\Documents and Settings\my username\My Documents\myoutput.out" lpt3:

If your printer is networked you can use the command

net use lpt1: \\computername\printer name /persistent to associate the printer with a virtual printer port. Use LPT3 to avoid conflicts with real lpt ports. You only need to do this once (the command net help use will give you the available options)



[navy]When I married "Miss Right" I didn't realise her first name was 'always'. LOL[/navy]
 
Just use the ShellExecute API.

Code:
    ShellExecute 0&, "PRINT", "D:\temp\jazz.txt", vbNullString, "D:\temp", SW_SHOWMINIMIZE


-David
2006 & 2007 Microsoft Most Valuable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
Thank you for your responses. Please keep in mind I am a pharmacist and not a programmer. That being said, I also have training in fortran.. I know, I know. I acutally learned fortran before the PC was invented. Ran programming off of punch cards. I guess that makes me a fossil of some sort.

I have not written a batch file in quite some time and I am not sure what to do with a Shellexecute API. HOWEVER, I appriciate the input.

I will attempt the "kludge" and report back.

Thanks again.

Birdpharmer
OSU Pharmacy College class of 1982
 
Well, Having a little trouble...no doubt you might have guessed this.

Lets assume the file I want to print is located at C:\documents and settings\owner\my documents\home\magiccube.pdf.

my batch file I created looks like this:

@echo off
echo
echo this batch file prints magic cubes
echo
copy/b "c:\docments and settings\owner\my documents\home\magiccube.pdf" lpt3:
echo
cls
exit


before I ran this batch file I went to a DOS command prompt, changed the directory to C:\documents and settings\owner\my documents\home . I the typed
copy/b magiccube.pdf lpt1:

One file was copied.

I then ran the batch file. BUt nothing happened. ????

I apologize if this is somewhat of a simplistic lesson, but that is how I have learned about computers over the past 25 years.

Thanks Againg

Birdpharmer
 
Your batch file may have failed because:-

1) it has a typo (it shows 'docments' instead of 'documents')

2) it shows LPT3:, not LPT1:. Do you actually have a printer connected to LPT3:?

I suggest you use a VBS script like the one below. Copy/paste the code into Notepad and save it as something like "pdf-print.vbs". The name is not important but the filename must end with ".vbs".

Change the list and the filenames to the ones that you have.

Code:
Set shellApp = CreateObject("Shell.Application")
title = "Leaflet Printer"
inputmsg = "This will let you print a leaflet. " & vbCRLF & vbCRLF & _
"Enter a number from the list below." & vbCRLF & vbCRLF & _
"  1 - Magic Cube" & vbCRLF & _
"  2 - Sunburn" & vbCRLF & _
"  3 - Depression" & vbCRLF & _
"  4 - Headaches" & vbCRLF & _
"  5 - Ingrown toenails"

strLeaflet = InputBox(inputmsg, title, "Enter an option")   

Select Case strLeaflet
Case 1
sFile = """C:\Documents and Settings\owner\my documents\home\magiccube.pdf"""
Case 2
sFile = """C:\Documents and Settings\owner\my documents\home\sunburn.pdf"""
Case 3
sFile = """C:\Documents and Settings\owner\my documents\home\depression.pdf"""
Case 4
sFile = """C:\Documents and Settings\owner\my documents\home\headaches.pdf"""
Case 5
sFile = """C:\Documents and Settings\owner\my documents\home\ingrown-toenails.pdf"""
End Select

shellApp.ShellExecute sFile,,,"print"
Wscript.sleep 1000
WScript.Quit()

Hope this helps...
 
What port is the printer on? LPT1 or 3?

You have typo's as Rick998 says.

You missed a step - You don't copy the pdf file - you copy the output file you generated earlier.

Print the document in the normal way - but tick the 'print to file' box and give the output file a name - lets use "myoutput.out" for now as the example. You only need to do this once.

i.e. open the file with adobe and print - but 'to file'. The file you create is the file you copy to the printer.

Assuming you printed magiccube to magiccube.out and your printer is on LPT1 then your batch file would be


@echo off
echo
echo this batch file prints magic cubes
echo
copy/b "c:\documents and settings\owner\my documents\home\magiccube.out" LPT1:
echo
cls
exit


I hope that makes it clear?

[navy]When I married "Miss Right" I didn't realise her first name was 'always'. LOL[/navy]
 
Well I was closer than I thought. And I think the problem is getting clearer.

Now,what if I an connected through a usb port to the printer and NOT through LPT1 or LPT3?

Thanks again.

Birdpharmer
 
This is where it gets a bit convoluted.

You need to share the printer and then associate the share with a port.

So if the printer is not shared, share it and then....

Once your printer is shared you can use the command.

net use lpt3: \\computername\printer name /persistent to associate the printer with a virtual printer port. Use LPT3 to avoid conflicts with real lpt ports. You only need to do this once (the command net help use will give you the available options)


[navy]When I married "Miss Right" I didn't realise her first name was 'always'. LOL[/navy]
 
The advantage of dglienna's suggestion - which I used in my VBS script - is that the ShellExecute method doesn't need to be told what printer port to use. As it's an API call it just uses whatever is set in the OS as the default printer.

In my opinion this makes it easier to use in the first place and the script doesn't have to be amended if, for example, the printer and/or the port it uses is changed.

As you are using MS Office then you could use the ShellExecute method from within a macro called from the Office toolbar.

Using your own example, try this. Open the macro editor, create a new module then copy/paste the code below into it.

Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, _
     ByVal lpFile As String, ByVal lpParameters As String, _
     ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub cmdMagicCube_Click()
    Dim magiccube As Long
    magiccube = ShellExecute(vbNull, "Print", "c:\documents and settings\owner\my documents\home\magiccube.pdf", "", "", vbNormalFocus)
End Sub

When you run the macro it will print the PDF document from within the MS Office app you called it from. You can just attach the macro to a toolbar button.

The disadvantage of this method is that you would need a seperate macro for each PDF doc. As a result it would be easier to create a form called from a toolbar button and use 'SelectCase' (as in my VBS script) in a macro to choose which PDF file to print using ShellExecute.

Hope this info helps...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top