【发布时间】:2012-03-03 07:56:47
【问题描述】:
我正在尝试使用 VBA 在 IE7 中自动化 JavaScript 表单/页面。 I have already determined 我必须使用 sendkeys 才能越过 JS 警报弹出窗口。由于此进程需要一些时间才能运行,因此此功能的目的是让用户能够在后台运行该进程并执行其他操作。这不是我在使用 sendkeys 时的首选选项,但我不知道我还有其他选择(除了强制用户等待进程完成,我可以将其用作最后的选择)。
为了防止发送键进入错误的应用程序的问题,我将焦点强制到当前的 IE 窗口。这样做时,焦点会集中在 IE 主窗口上,而不是警告框上。如果我尝试通过暂停代码来重置焦点,然后手动单击 IE 窗口,焦点会转到警报框。换句话说,使用代码设置焦点似乎是罪魁祸首。
我没有成功找到使用自动化捕获警报框的方法,否则我会尝试激活其上的按钮控件。
我正在使用此代码设置焦点:
Private Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Sub WaitForIE(myIEWindow As InternetExplorer, HWND As Long, Optional CheckForAlert As Boolean = False)
'OTHER CODE
While myIEWindow.Busy
DoEvents ' Wait until IE is done loading page and/or user actions are done.
Application.Wait DateAdd("s", 1, Now()) ' Wait one second per cycle so as not to use too much CPU
If CheckForAlert Then 'Only check for alert box when certain pages are loaded
Dim IEPage As Variant
Dim PlaceHolder As Long
Dim SearchFor As String
SearchFor = "userMessage = '"
Set IEPage = myIEWindow.Document.Frames(2).Document.Body
PlaceHolder = InStr(IEPage.innerhtml, SearchFor)
If PlaceHolder > 0 Then
If InStr(PlaceHolder + Len(SearchFor), IEPage.innerhtml, "'") > PlaceHolder + Len(SearchFor) + 1 Then ' if userMessage does not equal '' then the alert will show, so clear alert box
SetForegroundWindow HWND 'HWND is the HWND of the myIEWindow
Application.SendKeys "{ENTER}", True
End If
End If
Set IEPage = Nothing
End If
Wend
' MORE CODE
End Sub
此方法是否有替代方法(设置焦点),或者我缺少此方法的另一个步骤?
【问题讨论】:
标签: javascript internet-explorer vba automation sendkeys