Open and Print a PDF from Access

XelaIrodavlas

Registered User.
Local time
Today, 21:51
Joined
Oct 26, 2012
Messages
175
Hi All,

I'm trying to create a function to automatically open and print a PDF document from a hyperlink inserted in a field, unfortunately I am struggling with the .execWB method.

My code so far is as follows:
Dim MyBrowser As SHDocVw.InternetExplorer
Set MyBrowser = New SHDocVw.InternetExplorer
MyBrowser.Visible = True
MyBrowser.Navigate "C:\MyDocs\Example.pdf"
MyBrowser.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
Unfortunately while this is loading the PDF no problem, it returns an error on the ExecWB line:
Run-Time Error '-2147221248 (80040100)':
Method 'ExecWB' of object 'IWebBrowser2' failed
I have spent hours trying to Google an answer but am coming up blank.

Anyone got any ideas? or is there a different method I should use?

All thoughts and ideas appreciated :)

Thanks,
 
Ps. I am trying to print the page without opening the print dialogue because the Users will already have made a selection in the database front end. When this code works it will be used to print multiple documents per 'print' action :)
 
test for the state of the webbrowser between opening the pdf and printing it:

Do Until myWebBrowser.Object.ReadyState = acWebBrowserState.Complete
DoEvents
Loop
 
Last edited:
Hi Arnelgp,

Thanks for the quick reply, I've added your suggestion but now I'm getting a compile error: 'method or data member not found'. It is highlighting AcWebBrowserState.Complete

Is there a reference library I need to add for this? I have attached a screenshot of mine fyi.

Thanks,
 

Attachments

  • References.png
    References.png
    17.3 KB · Views: 216
Do Until Me.myWebBrowser.Object.ReadyState = AcWebBrowserState.acComplete
DoEvents
Loop
 
Same issue again, me.WebBrowser0 method or data member not found...

Getting closer though I will try messing with a few things :)

Code as it stands:
Code:
Dim MyBrowser As SHDocVw.InternetExplorer
Set MyBrowser = New SHDocVw.InternetExplorer
MyBrowser.Visible = True
MyBrowser.Navigate "C\ExampleDoc.pdf"
 Do Until Me.WebBrowser0.Object.ReadyState = AcWebBrowserState.acComplete
 DoEvents
 Loop
MyBrowser.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
 
use MyWebbrowser, since this is what you have.
 
Ah I think I see where the disparity is now and I realise I may have been a little unclear with my first post. The Browser is a separate instance of internet explorer opened from a command button, not a webBrowser Control.

I have continued searching and found some other suggestions how to check if Internet explorer is busy. If that's the only thing stopping the ExecWB from working then I should be able to work around it..

Code:
Dim MyBrowser As SHDocVw.InternetExplorer
Set MyBrowser = New SHDocVw.InternetExplorer
MyBrowser.Visible = True
MyBrowser.Navigate "P:\QAC Data Centre\WPAR's\WPAR  -  1 (C_1).pdf"
While MyBrowser.Busy
DoEvents
Wend
MyBrowser.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

Sadly, I am still getting the same error, but at least I am confident IE isn't busy when the ExecWB fires.

Will have to keep cracking on with this :)
 
i tested your code and as soon as it opens the pdf, the object myBrowser is disconnected.
i think the best we can do is display the pdf, then using separate function to print it.


'display the file
Dim MyBrowser As SHDocVw.InternetExplorer
Set MyBrowser = New SHDocVw.InternetExplorer
MyBrowser.Visible = True
MyBrowser.Navigate "P:\QAC Data Centre\WPAR's\WPAR - 1 (C_1).pdf"

'print it
ExecuteFile "P:\QAC Data Centre\WPAR's\WPAR - 1 (C_1).pdf"


on a new module paste the following code:

Public Enum actionType
OpenFile
PrintFile
End Enum
Public Const SW_SHOWNORMAL As Long = 1
#If Win64 Then
Private Declare PtrSafe Function ShellExecute Lib _
"shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As LongPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As LongPtr
#Else
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
#End If
Function ExecuteFile(fileName As String, action As actionType)
' action can be either "Openfile" or "Printfile".
Dim sAction As String
Select Case action
Case 0 ' openfile
sAction = "Open"
Case 1 ' printfile
sAction = "Print"
End Select
ShellExecute 0, sAction, fileName, vbNullString, "", SW_SHOWNORMAL
End Function
 
Hi Arnelgp,

Thanks again, I've tested your code (Edit: And it works perfectly!)

although I don't really know how ;)

I must admit this is now going well over my head, how does the ShellExecute know to interact with the web browser?

Sorry for being a pain :)
 
Last edited:
Hold the phone - looks like a couple of extra spaces were removed from the hyperlink when I copied your code back (I know, but I didn't create the document names...)

Anyway I added them back in and your code works perfectly! Thanks a tonne ArnelGP! :)
 
actually it doesn't know what the document is, it passed to the mime system (os) to determine which type of document it is.

it is a general purpose procedure, you can print any kind of document including mso docs.
 

Users who are viewing this thread

Back
Top Bottom