• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel Vba to open PDF and print

webmax

Member
Hi,

i am attaching the sample file with this. if i click the print button in the excel it opens the particular PDFs and print two copies. i need the macro code for this.
 

Attachments

  • Book2.xls
    24 KB · Views: 145
@webmax

Hi

Please try the below code in the normal module
`
Code:
Option Explicit
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
 
Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function
 
Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "C:\Users\Hello Baby\Desktop"
strFile = "abc.pdf"
 
printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub

`
Hope it is solve your problem other wise please inform us

Thanks

Patnaik :)
 
hi

when i run the above macro it throws the error attached
 

Attachments

  • errordoc2.xlsx
    509.6 KB · Views: 31
  • errordoc.xlsx
    509.5 KB · Views: 19
i think you was add the code in Worksheet please add the code in the Normal Module and try for your better knowledge please download the file and test if any issue let us know

Thanks

Patnaik
 

Attachments

  • Book2.xls
    39.5 KB · Views: 456
Hi,
Kindly find the below code.
Thisworkbook.followhyperlink "File Location "

Private Sub CommandButton1_Click()
ThisWorkbook.FollowHyperlink "C:\Users\Elcot\Desktop\test.pdf"
End Sub
 
Hi sgmPatnaik - It works fine. Thank you.

adding to that, I need to print different pdf pages. Say example if I put
1-2
5-6
9-10
13-14
17-18
21-22
25-26
........
........
in excel cell, it should print accordingly. Please help...
 
I've to append PDFs which are an output of my model. I m using PrimoPDF to do this which requires me to hit the print button in the PDF.

Using the above code I m able to normally print the PDF but not print into PrimoPDF so that I can append all and then Print.

Please help!
 
@Subageethan

Sorry for my late replay just i was out way from this Forum for long time so i didn't check any posts here, i think your problem may solve if not then please re-post with new question

@KapooK

Can you elaborate us what do you mean may be some body help with your problem or me

Thanks

SP
 
@webmax

Hi

Please try the below code in the normal module
`
Code:
Option Explicit
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

Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function

Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "C:\Users\Hello Baby\Desktop"
strFile = "abc.pdf"

printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub

`
Hope it is solve your problem other wise please inform us

Thanks

Patnaik :)
 
I tried your code to open and print a pdf file. It opened the file briefly, but didn't print. Could it be that I need to change the parameters after the file name? Would you please tell me what the parameters mean (0&,0&,3)?
Or give me any other hints why it doesn't print for me.

Thanks
 
Patnaik,

I used your code (Option Explicit, Public Function, ...Dim printThis) and it worked - very easy. Thank you. The only problem I'm having is though the file is closing, the application (Adobe Acrobat) stays open. I've tried different commands to close it, but, being a beginner, am beyond my league. What code would be necessary to close the Adobe application? See code below:


Code:
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "E:\SignOnForms-Data\FormsMisc"
strFile = "XXXXX.pdf"

printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub
Thanks in advance.
MJC
 
Last edited by a moderator:
iam sorry to replay this old post
i tried this code but it's not working for 64bit and give me this error

this code in this project must be updated for use on 64-bit systems

can anyone help me please
 
Hi, to make it work for 64bit, try changing:
Code:
Public Declare Function
to
Code:
Public Declare Ptrsafe Function

Stevie

If this was helpful, please click 'like' below.
 
Hey have you got any reply on this? is this solved? I am looking for same

Hi sgmPatnaik - It works fine. Thank you.

adding to that, I need to print different pdf pages. Say example if I put
1-2
5-6
9-10
13-14
17-18
21-22
25-26
........
........
in excel cell, it should print accordingly. Please help...
 
cvishu
Open a new thread from You own challenge as written in Forum Rules
... which You have read before Your the 1st posting here.
 
Back
Top