good afternoon!
I\'m trying to run a Macro that opens a website, login, select an option in the menu, fill a text-box and click another button.
So far, I can ope
Because the method IE.Document.getelementbyid("pCepNr").value
is returning error code 91
(Object not set), I suspect .getElementById
is failing to find an HTML element with id="pCepNr"
Please confirm this by trying this in your code:
' snip ...
IE.Document.parentWindow.execScript "enviaPage('mod_pesquisa/DisponibilidadeNaoClientes.asp', 'GET', 'True', 'dv_pagina', 'pUsrId=0000050007', 'True', 389, 892);"
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
Sleep (1000)
DoEvents
Wend
Dim problemTextBox as Object
Set problemTextBox = IE.Document.getelementbyid("pCepNr")
I suspect it will throw the same error. (EDIT See this question for the Sleep
routine.)
Unless you have the wrong id of the element you are looking for, the element simply doesn't exist when you try to access it.
If this script you are running ,
IE.Document.parentWindow.execScript "enviaPage('mod_pesquisa/DisponibilidadeNaoClientes.asp', 'GET', 'True', 'dv_pagina', 'pUsrId=0000050007', 'True', 389, 892);"
is what loads the element and the element doesn't exist before then, then your wait loop is failing to wait long enough for it to load.
I have noticed that for .asp pages IE.Busy
or IE.readyState
do not always work. You can test that by either stepping through slowing with the debugger and accessing the input box after you know it is loaded or just by forcing a large (10 second) sleep.
If simply your wait loop is exiting before the element is loaded, then you will need a better waiting routine. Here is one that I have used, parts of it are from other questions on SE.
Public Function ElementExists(document as Object, id As String) As Boolean
Dim el As Object
Set el = Nothing
On Error Resume Next
Set el = document.getElementById(id)
On Error GoTo 0
ElementExists = Not (el Is Nothing)
End Function
Public Function WaitForElement(document as Object, ByVal id As String, Optional ByVal timeout As Integer = 3) As Boolean
Dim start_time As Single
start_time = Timer
Do While Not (ElementExists(document, id)) And start_time + timeout > Timer
DoEvents
Loop
WaitForElement = ElementExists(document, id)
End Function
Calling WaitForElement(IE.Document, "pCepNr")
will wait no more than 3 seconds for the element to load and will return whether the element is accessible (true|false). 3 seconds is optional you can increase or decrease as you see fit eg (WaitForElement(IE.Document, "pCepNr", timeout:=10)
.