Webpage Automation using VBA

2019-09-06 09:27发布

问题:

I'm new to VBA and programming in general, and I have found some code online to automate webpage processing. Right now I'm just trying to log into a webpage for e-Oscar:

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now())
Loop

Set objCollection = IE.document.getElementsByTagName("input")
Set objName = IE.document.getElementsByTagName("name")

i = 0

While i < objCollection.Length
    If objCollection(i).ID = "companyId" Then

        ' Set text for search
        objCollection(i).Value = "companyId"

    Else
        If objCollection(i).ID = "userId" Then
           objCollection(i).Value = "uId"
    Else
        If objCollection(i).ID = "password" Then
           objCollection(i).Value = "pwd"
    Else
        If objCollection(i).ID = "securityMsgAck1" Then
           objCollection(i).Value = True
    'Else
        'If objName(i).Name = "securityMsgAck" Then
           'objName(i).Value = True
    'Else
        'If objCollection(i).ID = "button" Then
           'objCollection(i).Value = True


            ' "Search" button is found
            Set objElement = objCollection(i)
                        End If
                   End If
                End If
            End If
        'End If
    'End If

    i = i + 1
Wend
objElement.Click    ' click button to search

When ran AS-IS. This code opens up the webpage I'm trying to automate, fills out company id, user id, password, and seems to check the box that I acknowledge the T&C of the page. However, whenever I uncomment out "securityMsgAck" it no longer checks the box and gives me an error:

Run-time error '91': Object variable or With block variable not set.\

It also does this if I comment out "securityMsgAck" and uncomment out "button". It seems that my code is trying to check the box whenever these two pieces are not executed, but when they are the statement either fails before it can attempt to press it, or they are blocking it altogether. I'm not really sure... any advice would be appreciated. Thanks in advance.

Edit: Whenever I uncomment out either section of that code, my program also will no longer fill out company Id, userId, or Password, along with failing to check the security message box...

回答1:

to check the checkbox use the following code

 ElseIf objcollection(i).ID = "securityMsgAck1" Then
            objcollection(i).Checked = True