填充Webform时会打开多个窗口

时间:2013-08-14 14:22:17

标签: internet-explorer vba

我有以下代码在已经打开的Internet Explorer中填充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 

这段代码昨天工作正常。它找到带有pagetitle = Home的IE并填写表单。

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

代码有问题吗?

你认为我应该删除接下来的On error resume吗?

请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:0)

For Each IE in SWs似乎创建了3个Internet Explorer实例,我修改了ShellWindows而不是SHDocVW.ShellWindows的代码,并使用'SW.Count'来设置数量窗户检查。我还删除了on errors resume next行,因为它使调试变得更容易,如果有充分的理由,你可能想把它重新放入。

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