2016-02-09 247 views
0

我在MS Access 2010中使用以下代碼來打印HTML文件(本地)。等待ExecWB完成

它只有在我對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

嘗試DoEvents

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

這把執行控制傳送回給處理器,並允許恢復執行代碼之前的任何未決的系統任務的時間(如打印隊列)。

+0

有沒有人找到PowerShell的等效表達式? '雖然($ ie.Busy){Start-Sleep 1}'似乎沒有幫助。 – tresf

+0

@QZSupport應該工作 - 但也許測試'.ReadyState'屬性而不是'.Busy'。 –

+0

沒有運氣,或者... while(ie.Busy - 或$ ie.ReadyState -ne 4){Start-Sleep 1}'。調試COM對象時,不會出現ExecWB正確標記屬性(Windows 10,IE11 \) – tresf