无法使用VBA登录到多个页面

无法使用VBA登录到多个页面,vba,excel,Vba,Excel,任何人都可以用这个脚本来帮助我了解为什么我的第二个IE会话启动了,但是密码和用户的输入数据没有填充 Sub MyLogin() Dim Url As String Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True .navigate "https://website" Do Until .r

任何人都可以用这个脚本来帮助我了解为什么我的第二个IE会话启动了,但是密码和用户的输入数据没有填充

Sub MyLogin()
Dim Url As String
Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .navigate "https://website"

        Do Until .readystate = 4
            DoEvents
        Loop

        .document.all.Item("username").Value = "userid"
        .document.all.Item("password").Value = "password//"
        .document.forms(0).submit

        Url = "https://website"
        Set ie = CreateObject("InternetExplorer.Application")
        ie.Visible = True
        ie.navigate Url
        ie = Nothing

        Do
        DoEvents
        Loop Until ie.readystate = 4

        .document.all.Item("Enter user name").Value = "userid"
        .document.all.Item("passwd").Value = "password"
        .document.forms(0).submit

    End With
End Sub

在With块内重新指定参照时,不会获得新对象。随着ie的增加,您创建的第一个ie对象的引用计数将增加,并且块内以尊重开始的所有内容都将引用该对象。所以,当你这么做的时候

…从With块内部,以下代码行

…都引用了您创建的第一个InternetExplorer.Application对象,即ie引用的对象

此代码使用脚本说明此处发生的情况。为了便于说明,请使用字典:

Sub FooedUp()
    Dim foo As Scripting.Dictionary
    Set foo = New Scripting.Dictionary
    With foo                      'This now holds a reference to the 'foo' above.
        Debug.Print ObjPtr(foo)   'This is the pointer to that instance.
        .Add 1, 1                 'Add an item.
        Debug.Print .Count        'Count is now 1`.
        Set foo = New Scripting.Dictionary   'Re-use the 'foo' variable.
        Debug.Print ObjPtr(foo)   'The pointer is now different.
        Debug.Print .Count        '...but this is still bound to the first 'foo', prints 1.
    End With
End Sub
如果没有实际的URL,很难说,但您可能正在寻找更像这样的东西,而不是在第一次调用后进行清理:

Sub MyLogin()
    Dim Url As String
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .navigate "https://website"
        Do Until .readystate = 4
            DoEvents
        Loop
        .document.all.Item("username").Value = "userid"
        .document.all.Item("password").Value = "password//"
        .document.forms(0).submit
    End With   '<--Un-bind the first IE object.

    Url = "https://website"
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate Url
    With ie    '<--Bind the new IE object.
        Do
            DoEvents
        Loop Until ie.readystate = 4
        .document.all.Item("Enter user name").Value = "userid"
        .document.all.Item("passwd").Value = "password"
        .document.forms(0).submit
    End With
End Sub

这就解决了问题。谢谢你的帮助。
Sub FooedUp()
    Dim foo As Scripting.Dictionary
    Set foo = New Scripting.Dictionary
    With foo                      'This now holds a reference to the 'foo' above.
        Debug.Print ObjPtr(foo)   'This is the pointer to that instance.
        .Add 1, 1                 'Add an item.
        Debug.Print .Count        'Count is now 1`.
        Set foo = New Scripting.Dictionary   'Re-use the 'foo' variable.
        Debug.Print ObjPtr(foo)   'The pointer is now different.
        Debug.Print .Count        '...but this is still bound to the first 'foo', prints 1.
    End With
End Sub
Sub MyLogin()
    Dim Url As String
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .navigate "https://website"
        Do Until .readystate = 4
            DoEvents
        Loop
        .document.all.Item("username").Value = "userid"
        .document.all.Item("password").Value = "password//"
        .document.forms(0).submit
    End With   '<--Un-bind the first IE object.

    Url = "https://website"
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate Url
    With ie    '<--Bind the new IE object.
        Do
            DoEvents
        Loop Until ie.readystate = 4
        .document.all.Item("Enter user name").Value = "userid"
        .document.all.Item("passwd").Value = "password"
        .document.forms(0).submit
    End With
End Sub