0

我有以下代码在已打开的 Internet Explorer 中填写网络表单。

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 窗口。有时它会打开一个,甚至四个窗口。

这是代码有问题吗?

你认为我应该删除 On error resume next 吗?

请帮我。

谢谢。

4

1 回答 1

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
于 2013-08-24T08:18:34.547 回答