简体   繁体   中英

Using VBA to control IE to login to a website

I'm new to this, but I'm practising, and it seemed to me that logging into my Institute's website ought to be the easiest thing in the world. Right? Well, it's driving me crazy.

Specifically, the program fails at page.Document.getElementById("edit-name").Value = 1234

Run time error 424 "object required"

Also, and I'm not sure if this is related, when I type "page." the VBA editor helpfully provides a list of options, and I select Document. When I type the second "." to get "page.Document." I do not then get a further list of options.

Here's the complete program, and thanks for any help

Public Sub MyLogin()

'This uses early binding
'In menu at top of page - Tools...Reference.... Microsoft Internet Controls and Microsoft HTML Object Library must both be ticked



 Dim page As InternetExplorer
 Set page = New InternetExplorer
 page.Visible = True

 Dim MyLink As Object

'Login at Institute website
 page.Navigate "http://www.actuaries.org.uk/"


 Do While page.Busy Or page.ReadyState <> 4
   DoEvents
 Loop



 For Each MyLink In page.Document.Links
   If InStr(MyLink.href, "/user") Then MyLink.Click: Exit For
 Next MyLink

 Do While page.Busy Or page.ReadyState <> 4
    DoEvents
 Loop

 'Are now at the login page
 'Enter username, password and click


  page.Document.getElementById("edit-name").Value = 1234
  page.Document.getElementById("edit-pass").Value = "01/01/1977"
  page.Document.getElementById("edit-submit").Click




  End Sub

Specifically, the program fails at page.Document.getElementById("edit-name").Value = 1234

Nothing wrong with that line.

One reason that I can think of is that your For Loop is not executed. To avoid that, directly navigate to the relevant url . This trimmed version works for me.

在此处输入图片说明

Sub Sample()
    Dim sUrl As String: sUrl = "https://www.actuaries.org.uk/user"
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

    ie.navigate sUrl

    Do While ie.readystate <> 4: DoEvents: Loop

    ie.Document.getElementById("edit-name").Value = 1234
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM