Solved

How do I extract the Title of a web page

Posted on 2014-09-12
4
158 Views
Last Modified: 2014-09-13
I am using Excel VBA to scrape a page.
Everything is work, but I can't get the title.
HTMLDoc.Title is not returning anything, but HTMLDoc.body returns the text of the body.
0
Comment
Question by:rrhandle8
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40319943
Hi,

pls try

Sub ImportEE()

Dim ie As InternetExplorer
Dim html As HTMLDocument

Set ie = New InternetExplorer
ie.Visible = False
ie.navigate "http://www.experts-exchange.com/"


Do While ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

Set html = ie.document

MsgBox html.Title

Set ie = Nothing

End Sub

Open in new window

Regards
0
 

Author Comment

by:rrhandle8
ID: 40319998
Here is the code I am using:

   Dim HTMLDoc As New HTMLDocument
    Dim oHttp As MSXML2.XMLHTTP
    Dim sHTML As String
    Dim AnchorLinks As Object
    Dim TDelements As Object
    Dim TDelement As Object
    Dim AnchorLink As Object

    On Error Resume Next
    Set oHttp = New MSXML2.XMLHTTP
    If Err.Number <> 0 Then
        Set oHttp = CreateObject("MSXML.XMLHTTPRequest")
        MsgBox "Error 0 has occured while creating a MSXML.XMLHTTPRequest object"
    End If
    On Error GoTo 0
    If oHttp Is Nothing Then
        MsgBox "For some reason I wasn't able to make a MSXML2.XMLHTTP object"
        Exit Sub
    End If

    'Open the URL in browser object
    oHttp.Open "GET", URL, False
    oHttp.send
    sHTML = oHttp.responseText

    'Debug.Print oHttp.responseText

    HTMLDoc.body.innerHTML = oHttp.responseText
HTMLDoc.Title
0
 

Author Comment

by:rrhandle8
ID: 40320084
Any ideas?
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40320177
Following your code, you can extract the title from the responseText:

     HTMLDoc.Title = Mid(oHttp.responseText, InStr(1, oHttp.responseText, "<Title>", vbTextCompare) + 7, _
                    InStr(1, oHttp.responseText, "</Title>", vbTextCompare) - _
                    InStr(1, oHttp.responseText, "<Title>", vbTextCompare) - 7)

Open in new window


Regards,
-Glenn
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question