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

VBA Code to print a file 2

Status
Not open for further replies.

chewza

MIS
Feb 15, 2003
22
ZA
I am trying to write vba code to print (to a printer) a pdf file.

Any one know how??
 
Without a ACCESS pdf ADD-In I imagine you will have to write to a 'txt' file and then open the PDF engine as an APP.

rollie@bwsys.net
 
Use the API !

The ShellExecute function is a godsend for this. It basically helps with filetypes and extensions.

Put the following in your Declarations section...
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

...and in your code...

Code:
PrintAnything(strFile as String)
strAction = "print" ' or open, or whatever 
ShellExecute(0, strAction, strFile ,"" ,"" ,1)
End Function
 
comaboy,

I tried this with the declaration in module1 and the remainder in a clickbutton on click code. It compiled nicely and then did nothing. Not even a burp from the printer.

Any ideas. This looks like a great tool!!!

Rollie E
 
OK, I went off a bit half-cocked! That'll teach me, hehe. Try the code below. BTW, if the return code (x = shellexe..)
is less than 32, an error occured.



Code:
'In a module...
Public 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



' In your form...
'This code modified by comaboy from original code of...
    'KPD-Team 1998
    'URL: [URL unfurl="true"]http://www.allapi.net/[/URL]
    'E-Mail: KPDTeam@Allapi.net
    'Send an E-Mail to the KPD-Team
'The guys at allapi are sooo cool for their docs on the API!!
Sub PrintAnything(strFile As String)
    Dim x As Long
    strAction = "print" ' or open, or whatever
    x = ShellExecute(Me.hwnd, strAction, strFile, "", "", 1)
    MsgBox CStr(x)' see if an error occured!
End Sub

Private Sub Form_Load()
    PrintAnything "C:\Documents and Settings\Administrator\My Documents\cmcmapa4.pdf"
    PrintAnything "c:\test.doc" ' or whatever
    PrintAnything "c:\mapa4.pdf"' or whatever
    End
End Sub

Leave in the ALLAPI bits. I'd be lost without the API-Guide!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top