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

How to print an external PDF file from a Visual Basic Macro 1

Status
Not open for further replies.

rocco408

Programmer
Jun 7, 2004
14
0
0
Hello everyone, What would the code look like if I wanted to print an external PDF file from an Excel macro written in Visual Basic? Or maybe, how would I call a batch file from a a macro written in Visual Basic?
 
Take a look at the Shell function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I am using a pdf driver and trying to print a bunch of files with a VBA macro. However, when i print, a popu-up menu appears with the file save as screen. Sendkeys does not work. Does anyone have an idea of what i can do to automate this process.

Chris
 
I have searched all over the place for this and it seems that there are 3 solutions:

1. A timer solution for the dialog as found on MSDN (I didn't like this one at all)

2. Output the print job as a .prn file, then call distiller to "distill" the .prn files to .pdf files (search in tek-tips, I found a lot of these type of solutions, but didn't try any). Here is some code to get you started on how to output as .prn, but note that it is directly from Words VBA interface. To make it work in .net, all the "wd" commands have to be prefaced by other .net "wd" objects.

Code:
    Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Outputfilename:="C:\Temp\filename.prn", Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        Collate:=True, Background:=True, PrintToFile:=True, PrintZoomColumn:=0, _
        PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0

3. Call the print command from send keys (this is what I am doing thus far as it is the easiest solution, though it is definitely dirty.

Code:
'Declare a new word application instance.
Dim app As New Word.Application

'Declare a new word document and open the exported BLOB.
Dim Doc As Word.Document = app.Documents.Open(ThePath)

'Make the document visible
Doc.Application.Visible = True

app.ActivePrinter = "Acrobat PDFWriter"

'AppActivate is Critical.  It must match the caption of the
'Word instance that is open (for example, if the caption is
'FaxTemplate.Doc, you must put 'AppActivate("FaxTemplate.Doc").  I think there is a way to 'reference it differently, but haven't figured out yet
AppActivate(NameOfWordDoc)

'This sends a command to Ctrl P, Shift Tab, Shift Tab, Arrow 'Down, Enter, Type the Name of the Document, Enter".  This 'prints the current page only because of the 2 shift tabs
'and arrow down.
SendKeys.SendWait("^p+{TAB}+{TAB}{DOWN}~" & FileName & "~")
All in all, it's pretty crappy that there is no real clean solution to print pdf's in Word (at least that I can find). [banghead]

Hopefully this will help you and anyone else get in the right direction. If you find anything else, please let me know!
 
One thing I missed, In the sendkeys, you can put the path in as well and it doesn't matter what the default path Word has in the dialog box, it will save it to that path.

SendKeys.SendWait("^p+{TAB}+{TAB}{DOWN}~C:\Temp\MyDoc.Doc~")
 
Thank you all for your replies, I am no longer working on this problem. This issue is closed, although you may continue posting replies and comments if you choose.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top