3

First I'm very new to trying to automate IE via Excel VBA. That being said, I'm working to automate a login to a company-specific webpage (only accessible to our employees). The goal is to automate the login (employee number, password and click Login). I find Firefox to be particularly helpful in identifying fields so that's what I'm using in the screenshot.

I found some code online to navigate to a webpage and enter something into a search box. I've modified that as follows (the included link is not real).

Finally to the issue. If I enter a webpage like www.google.com for example, all will execute fine. But when I change to my company link, the code freezes at the Do While and I get the error shown. So my question is why it works for a general webpage but not for my company specific one? If I comment-out that line, I still get the disconnected error when debugging. Assuming that issue is an easy one to resolve, have I also properly identified the field?

Hopefully I've included enough info for you. If not, please let me know what else may be required. Thanks in advance for your help!

Error

Error

'start a new subroutine called SearchBot
Sub SearchBot()

'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser

'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "http://sampletext.asp"

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'enter value in the employee number box
objIE.document.getElementByName("txtEmployeeNum").Value = "123456"

Employee Number

Employee Number

aduguid
  • 3,099
  • 6
  • 18
  • 37

1 Answers1

2

Correct name of the method is getElementsByName.

You also want to operate on element of collection returned by this method, not whole collection. Using (0) index will allow to work on 1st element of collection.

Change:

objIE.document.getElementByName("txtEmployeeNum").Value = "123456"

to:

objIE.document.getElementsByName("txtEmployeeNum")(0).Value = "123456"

With such corrected code, you should step through code with F8 in VB Editor. For example by hovering over, see if objIE.Busy ever gets FALSE and especially if objIE.readyState ever reaches 4 - if only 3, try objIE.readyState < 3 instead.

EDIT:

Try replacing:

Dim objIE As InternetExplorer
Set objIE = New InternetExplorer

with:

Dim objIE As Object
Set objIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")

You may also need to change objIE.Navigate with objIE.Navigate2

Ryszard Jędraszyk
  • 2,296
  • 4
  • 23
  • 52
  • Thanks Ryszard. I made the update to getElementsByName...but still no luck with the error. I replaced Do While with an Application.Wait instead but to no avail. I still get the error "The object invoked has disconnected from its clients." Is there a way to re-activate an open IE11 window perhaps? – user9837831 May 24 '18 at 16:41
  • On which line do you get the disconnected error? What do you mean by reactivating an open IE window? This error usually shows up when IE is closed before doing operations, but I don't see any such thing in the code. – Ryszard Jędraszyk May 24 '18 at 17:01
  • I get the error on the line `Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop` If I hover over, it shows the disconnected error as well. I also tried to change to <3 and got the same error. – user9837831 May 24 '18 at 17:23
  • @user9837831 I updated my answer with what can solve this disconnecting problem. – Ryszard Jędraszyk May 24 '18 at 20:27
  • 2
    Works perfectly - `Dim objIE As Object Set objIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}") objIE.Visible = True objIE.navigate "http://sampletext.asp" Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop objIE.document.getElementsByName("txtEmployeeNum")(0).Value = "123456"` – user9837831 May 28 '18 at 01:01