等待ExecWB完成

时间:2016-02-09 16:23:29

标签: vba ms-access

我在MS Access 2010中使用以下代码来打印HTML文件(本地)。

只有当我取消评论msgbox(" wait")行时,它才会起作用。弹出打印对话框,我可以打印文档。 如果我不使用msgbox,脚本执行得太快以至于在ExecWB进行更改以启动打印对话框之前函数结束(结果为True)。如果没有打印对话框,则不会将文档发送到打印机。

有没有办法检查ExecWB是否已完全执行。我尝试在循环中检查objIE.busy值,但这也不起作用。

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

1 个答案:

答案 0 :(得分:1)

尝试DoEvents

objIE.ExecWB 6, 1, 0, 0
While objIE.Busy
    DoEvents
Wend

这会将执行控制传递回处理器,并允许在恢复执行代码之前发生任何挂起的系统任务(例如打印队列)。