Celine N
Celine N

Reputation: 135

How is it possible to get the title of a webpage using vba?

Here is the code i wrote to get the titles of all the opened internet explorer pages on my computer.

Dim objShell As Object
Dim IE_count As Variant
Dim my_url As Variant
Dim my_title As Variant
Dim x As Integer

    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    MsgBox ("The number of pages is: " & IE_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
    MsgBox ("The title of this page is: " & my_url)

The first MsgBox in my piece of code is actually displaying the correct number of IE_count (representing the number of windows opened).

However, I am having some trouble displaying the my_url or my_title variables. The MsgBox related to these fields is just showing "The title of this page is: " and nothing after the "is".

Do you know how to fix this error in order to get the url or the title of a window?

PS: i even tried changing the data types from variant to string for my_url and my_title but nothing changed.

I would really appreciate any help! Thank you :)

Upvotes: 0

Views: 512

Answers (1)

Justin Lane
Justin Lane

Reputation: 36

Try removing this line:

Dim x As Integer

Edit:

What version of Excel are you using?

Perhaps '''Dim x As Variant''' will work.

Upvotes: 1

Related Questions