Reputation: 6020
I am using the following code in MS Access 2010 to print an HTML file (local).
It will only work if I de-comment the msgbox("wait") line. A print dialog pops up and I can print the document. If I don't use the msgbox, the script executes so fast that the function ends (with True as result) before ExecWB had the change to start the printing dialog. Also without the printing dialog the document is not send to a printer.
Is there a way to check if ExecWB has been completely executed. I tried checking the objIE.busy value in a loop but that does not work either.
Public Function PrintHTML(filePath As String, Optional visibleBrowser As Boolean = False) As Boolean
'-------------------------------------------------------------------------------
' Procedure : PrintHTML
' DateTime : 12/31/2007 12:53 PM 12:53
' Author : Aaron Bush
' Purpose : To print a html file.
' Input(s) : filePath - The path to the file you wish to print.
' Output(s) : True - No error encountered.
' False - Error found.
' Remarks : Free for public use.
' Requires reference to Microsoft Internet Controls. to set a
' reference, go to Tool, References, and select
' "Microsoft Internet Controls". If library is not present then
' click "browse" and browse to C:\Windows\System32\shdocvw.dll.
'-------------------------------------------------------------------------------
Dim objIE As SHDocVw.InternetExplorer
On Error GoTo Err_Hnd
'Instantiate a instance of Internet Explorer:
Set objIE = New SHDocVw.InternetExplorer
'Set visibility:
objIE.Visible = visibleBrowser
'Load specified file:
objIE.Navigate filePath
'Wait for file to load:
Do Until objIE.ReadyState = READYSTATE_COMPLETE
Loop
'Print:
objIE.ExecWB 6, 1, 0, 0
'MsgBox ("wait")
'Flag as error free:
PrintHTML = True
Exit_Proc:
On Error Resume Next
'Close browser:
objIE.Quit
Exit Function
Err_Hnd:
VBA.MsgBox "Error " & VBA.Err.Number & " in procedure PrintHTML of Module" & m_strModuleName_c & vbNewLine & VBA.Err.Description, vbMsgBoxSetForeground Or vbSystemModal, "Error - " & m_strModuleName_c & ".PrintHTML"
Resume Exit_Proc
End Function
Upvotes: 0
Views: 1055
Reputation: 17647
Try DoEvents
objIE.ExecWB 6, 1, 0, 0
While objIE.Busy
DoEvents
Wend
This passes execution control back to the processor and allows any pending system tasks to take place (such as a print queue) before resuming the executing code.
Upvotes: 1