getElementByID with Internet Explorer using Shell Windows

LewisHoulden

New member
Local time
Today, 18:03
Joined
Jul 16, 2013
Messages
4
My first post! I have used this forum many times to help solve VBA queries but cannot seem to find an answer to my query: :banghead:

Ok, so I can currently open a new internet explorer instance, navigate to my URL, then use the .getElementByID to insert my preference into a textbox on the page. Looks like this:

Code:
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate "URL of my Choice"
Do While oIE.Busy Or oIE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop   
        oIE.Document.getElementById("Surname").Value = "My Surname Here"

Now, that works fine. I want to take this a step further and use an existing internet explorer window. I can find the already existing IE window, open a new tab to same URL as previous, but I am stopped when it tries to use getElementbyID. This gives me the error "91 Object variable or With block variable not set." I cannot understand what's wrong considering I can use .Navigate but not .getElementbyID!!! :banghead:

My Code:
Code:
Function testme()
    Dim objWindow As Object
    Dim objIEApp As Object
    Dim objShell As Object
    Dim objItem As Object
    Set objShell = CreateObject("Shell.Application")
    Set objWindow = objShell.Windows()
    For Each objItem In objWindow
    Debug.Print objItem
        If LCase(objItem.FullName Like "*iexplore*") Then
        w.Navigate "URL of my choice" , CLng(2048)
        w.Document.getElementById("Surname").Value = "My Surname Here"
        got skiprest
        End If
    Next objItem
skiprest:
   
End Function

Your help with this will be much appreciated!

Thanks
 
welcome to the forum

Your object w is neither declared nor ever assigned a value, that I can see.
 
Thanks for the reply.

Sorry, I must of cut the wrong code. Let me show you it in full. Ignore previous code. This is the one I am working with.

It's part of a loop:

Dim w As Object
Dim Title as String
Title = "My web page here"
For Each w In CreateObject("Shell.Application").Windows
If w.Name = "Windows Internet Explorer" Then 'if the shell window is IE, continue...

If w.LocationName Like (Title & "*") Then 'Find the IE window I want,


w.Visible = True 'this works
w.Navigate "My URL", CLng(2048) 'this works too. Opens in new tab

w.Document.getElementById("Surname").Value = "My surname

End If
End If
Next
 
So what type of object is w then? Probably not the type you think. Try . . .
Code:
Dim w As Object
For Each w In CreateObject("Shell.Application").Windows
   msgbox Typename(w)
Next
. . . and what do you get? Probably not an InternetExplorer.Application?
 
Thank you for your response.

It is not InternetExplorer.Application, msgbox = IWebBrowser2.

How do I switch between the two?

Basically, I have found the internet explorer window I require, navigated to a new URL in a new tab, but cannot use getElementByID which I was previously able to use (see first post).

I can't understand how I am able to use w.Navigate but cannot use w.getElementByID.

Any ideas?

Thanks again.
 
Is getElementByID a method of the Document object contained by the WebBrowser object? The code in your first post is . . .
Code:
w.Document.getElementByID()
 
I was referring to this code in my first post:

Code:
Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Navigate "URL of my Choice"
Do While oIE.Busy Or oIE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop   
        oIE.Document.getElementById("Surname").Value = "My Surname Here"

The above works without fault. It updates a TextBox on the loaded webpage. The only difference is the above code opens a new internet explorer window, whereas the code I want is to find an existing internet explorer window, open the webpage in a new tab then update the TextBox but it falls over at the TextBox part :banghead:

This is driving me mad!
 
You are doing this . . .
Code:
webBrowser.getElementByID
. . . but isn't getElementByID a member, not of the WebBrowser class but of the document class it contains, so . . .
Code:
webBrowser.Document.getElementByID
Do you see the difference? Look again at the code that works . . .
Code:
w.Navigate "URL of my choice" , CLng(2048)
w[COLOR="DarkRed"].Document[/COLOR].getElementById("Surname").Value = "My Surname Here"
 

Users who are viewing this thread

Back
Top Bottom