Internet explorer 填写Web表单时打开多个窗口

Internet explorer 填写Web表单时打开多个窗口,internet-explorer,vba,Internet Explorer,Vba,我有以下代码在一个已经打开的InternetExplorer中填写webform Sub L() Dim IE As New SHDocVw.InternetExplorer Dim SWs As New SHDocVw.ShellWindows Dim PageTitle As String On Error Resume Next u = 1 'Looks at all windows in Windows For Each IE In SWs I

我有以下代码在一个已经打开的InternetExplorer中填写webform

Sub L()

Dim IE As New SHDocVw.InternetExplorer
Dim SWs As New SHDocVw.ShellWindows
Dim PageTitle As String
On Error Resume Next
    u = 1
    'Looks at all windows in Windows
    For Each IE In SWs

        If (LCase(IE.FullName) Like "*iexplore*") = True Then
            If u <> 1 Then
                u = u + 1
            End If
 End If
 PageTitle = IE.Document.Title
        If InStr(PageTitle, "Home") Then

IE.Navigate "http://intranet.com/Webforms/TaskCreation.aspx"
Do 'Wait till the edit page is loaded.
Loop Until IE.ReadyState = READYSTATE_COMPLETE And Not IE.Busy

IE.Document.getElementById("Content_TEXT_ALPHA_COLUMN1").Value = "A"
IE.Document.getElementById("Content_TEXT_ALPHA_COLUMN2").Value = "B"
IE.Document.getElementById("Content_TEXT_ALPHA_COLUMN14").Value = "C"

IE.Document.Forms(0).Item("Content_ddlRegion").Value = "28"
IE.Document.Forms(0).Item("Content_ddlSubRegion").Value = "33"
IE.Document.Forms(0).Item("Content_ddlPriority").Value = "23"
IE.Document.Forms(0).Item("Content_ddlClientCode").Value = "37"
IE.Document.Forms(0).Item("Content_ddlCompanyCode").Value = "62"
IE.Document.Forms(0).Item("Content_DDL_COLUMN1").Value = "5978"
IE.Document.Forms(0).Item("Content_DDL_COLUMN2").Value = "5966"
IE.Document.Forms(0).Item("Content_ddlActivity").Focus
IE.Document.Forms(0).Item("Content_ddlActivity").Value = "13720"
IE.Document.Forms(0).Item("Content_ddlActivity").FireEvent ("onchange")

While IE.Busy
DoEvents
Wend
Application.Wait Now() + TimeValue("00:00:03")


IE.Document.Forms(0).Item("Content_ddlSubActivity").Value = "13735"

IE.Document.getElementById("Content_imgRequestRcvdDate").Click

IE.Visible = True

 End If

    Next IE

End Sub 
subl()
Dim IE作为新的SHDocVw.InternetExplorer
将SWs调暗为新SHDocVw.SHELL窗口
将页面标题设置为字符串
出错时继续下一步
u=1
'查看windows中的所有窗口
对于SWs中的每个IE
如果(LCase(IE.FullName)像“*iexplore*”)=True,那么
如果你是1,那么
u=u+1
如果结束
如果结束
PageTitle=IE.Document.Title
如果是InStr(页面标题“主页”),则
即“导航”http://intranet.com/Webforms/TaskCreation.aspx"
请等待编辑页面加载。
循环直到IE.ReadyState=ReadyState\u完成且IE不忙
IE.Document.getElementById(“Content\u TEXT\u ALPHA\u COLUMN1”).Value=“A”
IE.Document.getElementById(“Content\u TEXT\u ALPHA\u COLUMN2”).Value=“B”
IE.Document.getElementById(“Content\u TEXT\u ALPHA\u COLUMN14”).Value=“C”
IE.Document.Forms(0).项(“内容区域”).Value=“28”
IE.Document.Forms(0).项(“内容”).Value=“33”
IE.Document.Forms(0).项(“内容优先级”).Value=“23”
IE.Document.Forms(0).Item(“内容\客户端代码”).Value=“37”
IE.Document.Forms(0).Item(“内容公司代码”).Value=“62”
IE.Document.Forms(0).项(“内容”DDL\u COLUMN1”).Value=“5978”
IE.Document.Forms(0).项(“内容”DDL\u COLUMN2”).Value=“5966”
IE.Document.Forms(0).项目(“内容/活动”).焦点
IE.Document.Forms(0).项(“内容”\u活动”).Value=“13720”
IE.Document.Forms(0).项(“内容/活动”).FireEvent(“变更”)
趁我忙
多芬特
温德
Application.Wait Now()+时间值(“00:00:03”)
IE.Document.Forms(0).Item(“内容子活动”).Value=“13735”
IE.Document.getElementById(“Content\u imgRequestRcvdDate”)。单击
可见=真实
如果结束
下一个
端接头
这个代码昨天运行良好。它找到pagetitle=Home的IE并填写表单

虽然我没有完全改变任何东西,但今天我遇到了一个错误:当宏完成执行时,它会打开新的Internet Explorer窗口。 有时它会打开一个甚至四个窗口

代码有问题吗


你认为我下一步应该删除错误吗

请帮帮我


谢谢。

SWs中每个IE的
行似乎创建了3个internet explorer实例,我修改了
ShellWindows
的代码,而不是
SHDocVW.ShellWindows
,并使用“SW.Count”设置要检查的窗口数。我还删除了“下一步继续执行错误时的
”行,因为它使调试更容易,如果有充分的理由,您可能希望将其放回原处

Sub L()

Dim IE As InternetExplorer
Dim SWs As ShellWindows
Dim PageTitle As String
Dim iCounter As Integer
Dim u As Integer

    Set SWs = New ShellWindows
    If SWs.Count > 0 Then
        For iCounter = 1 To SWs.Count
            Set IE = SWs.Item(iCounter - 1)

            If (LCase(IE.FullName) Like "*iexplore*") = True Then
                    If u <> 1 Then
                        u = u + 1
                    End If


                PageTitle = IE.Document.Title
                    If InStr(PageTitle, "Home") Then

                    IE.Navigate "http://intranet.com/Webforms/TaskCreation.aspx"
                    Do 'Wait till the edit page is loaded.
                    Loop Until IE.ReadyState = READYSTATE_COMPLETE And Not IE.Busy

                    IE.Document.getElementById("Content_TEXT_ALPHA_COLUMN1").Value = "A"
                    IE.Document.getElementById("Content_TEXT_ALPHA_COLUMN2").Value = "B"
                    IE.Document.getElementById("Content_TEXT_ALPHA_COLUMN14").Value = "C"

                    IE.Document.Forms(0).Item("Content_ddlRegion").Value = "28"
                    IE.Document.Forms(0).Item("Content_ddlSubRegion").Value = "33"
                    IE.Document.Forms(0).Item("Content_ddlPriority").Value = "23"
                    IE.Document.Forms(0).Item("Content_ddlClientCode").Value = "37"
                    IE.Document.Forms(0).Item("Content_ddlCompanyCode").Value = "62"
                    IE.Document.Forms(0).Item("Content_DDL_COLUMN1").Value = "5978"
                    IE.Document.Forms(0).Item("Content_DDL_COLUMN2").Value = "5966"
                    IE.Document.Forms(0).Item("Content_ddlActivity").Focus
                    IE.Document.Forms(0).Item("Content_ddlActivity").Value = "13720"
                    IE.Document.Forms(0).Item("Content_ddlActivity").FireEvent ("onchange")

                    While IE.Busy
                        DoEvents
                    Wend
                    Application.Wait Now() + TimeValue("00:00:03")


                    IE.Document.Forms(0).Item("Content_ddlSubActivity").Value = "13735"

                    IE.Document.getElementById("Content_imgRequestRcvdDate").Click

                    IE.Visible = True

                End If 'If InStr(PageTitle, "Home")
            End If 'If (LCase(IE.FullName) Like "*iexplore*") = True
        Next iCounter


    End If 'If SWs.Count > 0 Then

Set IE = Nothing
Set ws = Nothing

End Sub
subl()
Dim IE作为InternetExplorer
将SWs变暗为壳窗
将页面标题设置为字符串
作为整数的Dim I计数器
将u定为整数
设置SWs=新外壳窗口
如果SWs.计数>0,则
对于iCounter=1到SWs.Count
设置IE=SWs.Item(iCounter-1)
如果(LCase(IE.FullName)像“*iexplore*”)=True,那么
如果你是1,那么
u=u+1
如果结束
PageTitle=IE.Document.Title
如果是InStr(页面标题“主页”),则
即“导航”http://intranet.com/Webforms/TaskCreation.aspx"
请等待编辑页面加载。
循环直到IE.ReadyState=ReadyState\u完成且IE不忙
IE.Document.getElementById(“Content\u TEXT\u ALPHA\u COLUMN1”).Value=“A”
IE.Document.getElementById(“Content\u TEXT\u ALPHA\u COLUMN2”).Value=“B”
IE.Document.getElementById(“Content\u TEXT\u ALPHA\u COLUMN14”).Value=“C”
IE.Document.Forms(0).项(“内容区域”).Value=“28”
IE.Document.Forms(0).项(“内容”).Value=“33”
IE.Document.Forms(0).项(“内容优先级”).Value=“23”
IE.Document.Forms(0).Item(“内容\客户端代码”).Value=“37”
IE.Document.Forms(0).Item(“内容公司代码”).Value=“62”
IE.Document.Forms(0).项(“内容”DDL\u COLUMN1”).Value=“5978”
IE.Document.Forms(0).项(“内容”DDL\u COLUMN2”).Value=“5966”
IE.Document.Forms(0).项目(“内容/活动”).焦点
IE.Document.Forms(0).项(“内容”\u活动”).Value=“13720”
IE.Document.Forms(0).项(“内容/活动”).FireEvent(“变更”)
趁我忙
多芬特
温德
Application.Wait Now()+时间值(“00:00:03”)
IE.Document.Forms(0).Item(“内容子活动”).Value=“13735”
IE.Document.getElementById(“Content\u imgRequestRcvdDate”)。单击
可见=真实
如果‘如果仪表’结束(页面标题“主页”)
如果'If(LCase(IE.FullName)像“*iexplore*”)=True,则结束
下一个iCounter
如果“如果SWs.Count>0,则结束”
设置IE=无
设置ws=Nothing
端接头

“你认为我应该在下一步删除错误恢复吗?”好吧,让我们看看。当你尝试这样做时会发生什么?它解决了还是改变了问题?我出错了。运行时错误:自动错误,未指定的错误。Ken,通过调试每一行我了解到的是:即使宏在标题旁边找到了窗口并执行了操作,它仍然继续运行,寻找更多具有相同标题的窗口。它必须是找到第一个后停止的一种方式。我不确定我是否说得够清楚了。你的问题根本不清楚。:-)运行
IE.Visible=True
行后(在其后的
结束(如果
)之前),为
添加一个
退出,因为这意味着您已经找到(并处理)了要更新的浏览器实例。(你还应该学会正确缩进