Thanks Thanks:  0
Likes Likes:  0
Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42

Thread: Read HTML Source Code with VBA

  1. #1
    New Member
    Join Date
    Jun 2011
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Read HTML Source Code with VBA

    Using VBA, I am trying to retrieve the contents of the Source of a web page (the same as would appear if you right-clicked on the page and chose "View Source") into a variable so I can work on it in VBA (using InStr, etc.).

    The problem is I can use code such as
    strHTMLText = ie.Document.body.innerText
    or
    strHTMLText = ie.Document.body.outerText
    to retrieve the code, but in either case only part, not all, of the source code is captured. I need ALL the code. Is there some kind of code such as ie.Document.body.allText or similar that would perform this function?

    Much obliged!

  2. #2
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,251
    Post Thanks / Like
    Mentioned
    32 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Read HTML Source Code with VBA

    What code aren't you getting with that?

    It should return all the HTML of the element you specify.

    How are you getting it anyway?

    You mention a string variable, in VBA the String data type does have a limit of characters it can hold.
    If posting code please use code tags.

  3. #3
    New Member
    Join Date
    Jun 2011
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read HTML Source Code with VBA

    Norie,

    Thanks for the quick response.

    When I use ie.Document.body.innerText, all I apparently get is the text within the Body element. What I'm trying to get to is this portion of HTML within the Body element of the Source Page (none of it shows up with innerHTML):

    <Aclass=noteshref=FootNotes.asp?FNtsID=7947***********="return overlib('87 Peter here warned the believers who were suffering...', CAPTION, '1 Peter 5', FGCOLOR, '#fffff0', BGCOLOR, '#8b0000');"**********="return nd();">7A>

    Can you give any light how I can access it?

    Thanks again.

  4. #4
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,251
    Post Thanks / Like
    Mentioned
    32 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Read HTML Source Code with VBA

    I don't suppose you can post the URL?

    Without that it's hard to tell what might be happening.
    If posting code please use code tags.

  5. #5
    New Member
    Join Date
    Jun 2011
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read HTML Source Code with VBA

    Sorry I didn't format the last post properly. Here's a URL you can use:

    http://online.recoveryversion.org/bi...2901&lvid=2901

    If you view the source (right-click, "View Source"), I am trying to access the following within the source:

    href=FootNotes.asp?FNtsID=2353

    so I can determine the footnote number ID (such as 2353) for a particular footnote.

    Hope that clarifies a little.

    Thanks for the help!

  6. #6
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,251
    Post Thanks / Like
    Mentioned
    32 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Read HTML Source Code with VBA

    Is that all you want, not the full code?
    If posting code please use code tags.

  7. #7
    New Member
    Join Date
    Jun 2011
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read HTML Source Code with VBA

    There are often several snippets of code within a page in the form "href=FootNotes.asp?FNtsID=2353" (with only the last numeric digits different, though they can be in length of one digit to four digits as here). I.e., FNtsID can be from 1 to 9999.

    If I were able to fetch the first FootNotes reference on a page, that would be sufficient. If it is involves getting HTML text on either side of the the reference, I can work with that using InStr.

    Thanks!

  8. #8
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,251
    Post Thanks / Like
    Mentioned
    32 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Read HTML Source Code with VBA

    You can get the full html using document.outerHTML.

    I'm afraid I can't help much further though - this page seems copyrighted in some way.

    Not sure exactly how, but that doesn't quite matter.

    I'll give you a couple of hints though - everything you seem to be looking for is in the links on the page.

    You can indentify the ones you want by looking at the value of their class attribute, it's 'notes' for the numbers and 'ref' for the letters.
    If posting code please use code tags.

  9. #9
    MrExcel MVP ZVI's Avatar
    Join Date
    Apr 2008
    Location
    Sevastopol
    Posts
    3,393
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Read HTML Source Code with VBA

    Try this code and see result in Immediate window:
    Code:
    
    Sub Test()
      Const URL$ = "http://online.recoveryversion.org/bibleverses.asp?fvid=2901&lvid=2901"
      Const MASK$ = "href=FootNotes.asp?FNtsID="
      Dim txt As String, i As Long
      With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .Send
        txt = .ResponseText
      End With
      Do
        i = InStr(i + 1, txt, MASK)
        If i = 0 Then Exit Do
        Debug.Print Val(Mid$(txt, i + Len(MASK), 15))
      Loop
    End Sub
    Vladimir Zakharov
    Microsoft MVP Office Apps & Services

  10. #10
    New Member
    Join Date
    Jun 2011
    Posts
    9
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Read HTML Source Code with VBA

    Norie,

    When I tried the document.outerHTML I got a Run-time error 438: Object doesn't support this property or method. I think your hints about looking up the value of the class attribute, etc. would have worked if I had gone on to try them out.

    ZVI (was that Vladimir?),

    Your suggestion worked fantastic! I'll see how I can incorporate it in my code.

    Both - Thanks for taking time and effort to reply. Best to you!

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •