0

既に開いている Internet Explorer で Web フォームに入力する次のコードがあります。

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 ウィンドウが開きます。ときどき、1 つ、場合によっては 4 つのウィンドウが開きます。

これはコードに何か問題がありますか?

On error resume next を削除する必要があると思いますか?

私を助けてください。

ありがとう。

4

1 に答える 1

0

行は Internet Explorer の 3 つのインスタンスを作成するように見えます。代わりにFor Each IE in SWsコードを変更し、「SW.Count」を使用してチェックするウィンドウの数を設定しました。また、デバッグを容易にするためにこの行を削除しました。正当な理由がある場合は、元に戻すことをお勧めします。ShellWindowsSHDocVW.ShellWindowson 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 に答える