2012-03-02 50 views
0

我想在IE7中使用VBA自动化JavaScript表单/页面。 I have already determined我必须使用sendkeys移过JS弹出窗口。由于此过程需要一段时间才能运行,因此此功能的目的是让用户能够在后台运行该过程并执行其他操作。在使用sendkeys时,这不是我的首选选项,但我不知道我有另一种选择(除了强制用户等待流程完成,我可以将其用作最后的选项)。JavaScript警报()没有得到焦点在IE

为了防止的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 

是否有这种方法的替代方案(设置聚焦),或是否有另一步骤该方法我丢失?

回答

0

诀窍是将您的{} ENTER,这将带来焦点警告框

Application.SendKeys "{TAB}{ENTER}", True 

应该工作之前发送{TAB}。

+0

工作就像一个魅力!非常感谢。 – Gaffi 2012-03-05 13:06:47

相关问题