0

So I've been trying to navigate to an already opened IE window using VBA. I've found a post explaining what seems to be how to do this. However, my main problem now is that I'm not too familiar with VBA and my website in particular, doesn't seem to have a document title. All I have in the html code when i inspect it in the Debugger is the following:

<title></title>

Am I missing something here? Is there any other way to refer to the website that may be easier or simpler?

Sub demo()

Dim str_val As Object
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title

    If my_title Like "XYZ" & "*" Then
        Set IE = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

If marker = 0 Then
 MsgBox ("A matching webpage was NOT found")

Else
    MsgBox ("A matching webpage was found")

    Set str_val = IE.document.getElementById("txtbox1")
    str_val.Value = "demo text"

End If
End Sub

Reference

VBA code to interact with specific IE window that is already open

QHarr
  • 83,427
  • 12
  • 54
  • 101
Joel Bastien
  • 121
  • 2
  • 11
  • 3
    `On Error Resume Next` will be stopping any errors, try taking this out and seeing if you get an error that you need to handle. Also, what is the object `IE` defined as? – Nathan_Sav Mar 14 '19 at 13:55
  • you should have a document.url you can use – QHarr Mar 14 '19 at 14:10
  • How would i find the document.url when i inspect the html code of the website? I would replace the document.title by document.url, correct? – Joel Bastien Mar 14 '19 at 15:41
  • [Duplicate Thread] You should not create a duplicate threads for the same issue. https://stackoverflow.com/questions/55128737/how-to-navigate-to-an-already-opened-internet-explorer-window-vba You need to continue your discussion on the same thread. – Deepak-MSFT Mar 15 '19 at 02:32

1 Answers1

0

This is the way I normally handle said problem,

Sub GetURLs()

'   Uses MS HTML Controls
'   and MS Internet Controls

Dim s As SHDocVw.InternetExplorer
Dim sw As SHDocVw.ShellWindows
Dim d As MSHTML.HTMLDocument

Set sw = New SHDocVw.ShellWindows

For Each s In sw
    If TypeName(s) = "IWebBrowser2" And s.Name <> "Windows Explorer" Then
        Debug.Print s.Name, s.LocationURL
        Set d = s.Document
        Debug.Print d.getElementsByTagName("TD")(0).innerhtml
    End If
Next s

Set sw = Nothing
Set s = Nothing

End Sub
Nathan_Sav
  • 8,466
  • 2
  • 13
  • 20
  • Hi thanks a lot for this! However, when I run it, it gives me a type: mismatch error at the line Set d = s. document ... I've just copied/pasted your code into VBA. Is there something that I'm missing that I would need to do/change in the code to fit my website/purpose? – Joel Bastien Mar 14 '19 at 15:54