[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Analyzing HTML documents

Posted on 2002-05-03
Medium Priority
Last Modified: 2010-05-02

I want to analyze some html files (stored locally) and to be able to retrieve all the links, images or whatever other tags.
Now I do this by loading the file into a webbrowser control, then I can read
The problem is many files have images inserted with absolute http:// paths, try to connect to another websites(like visit counters etc), display alerts, prompts or confirmation boxes, script errors and so on.
I want to analyze the file without the user to see anything. But if he's offline, having anything that tries to access the web may have undesired results like launching phone dialers, error messages, etc. If he's online, the file will be loaded slowly because it accesses online stuffs.
Also there is no way to prevent alerts and prompts to appear. Setting offline and silent properties to True has no effect.

So my question is can I analyze a Html file without loading it into a webbrowser(or how to avoid the above problems if I use a webbrowser).
Of course I don't mean a substring search solution, like to look for "<A HREF", then to look for the closing ">" etc.

Question by:hveld
  • 4
  • 2
LVL 28

Accepted Solution

AzraSound earned 800 total points
ID: 6988492
Try this:

- Set a reference to Microsoft HTML Object Library
- Load your HTML file into a string buffer using native VB file commands
- Use code similar to the following to generate an HTMLDocument object you can analyze

Public Function GetHTMLDocument(ByVal HTMLCode As String) As HTMLDocument
    Dim htmlDoc   As New HTMLDocument

    htmlDoc.body.innerhtml = HTMLCode
    Set GetHTMLDocument = htmlDoc
End Function
LVL 16

Expert Comment

ID: 6988543

Author Comment

ID: 6989259
that's OK, thanks!
Works fine!
But this way the URL of the file to analyze is about:blank because htmlDoc is created from a string.
So I can't get absolute local paths to images or linked files, if they are included in the html document with relative paths.
Of course I can try to obtain the abs. local paths to them, having the path to the file being analyzed - but this means to change a lot of already tested code.
I tried to set
htmlDoc.location = "local_path_to_the_file"
but this opens the file in IE.
If the first image in the html file is Image1.jpg,
but I need the full local path to the image
Can this be done?
If no, I'll accept your solution as an answer and will rewrite my code
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 28

Expert Comment

ID: 6989920
Perhaps you can alter the function to strip the about:blank statements for you, since we know this is a limitation of loading an html document in this way.

Public Function GetHTMLDocument(ByVal HTMLCode As String) As HTMLDocument
    Dim htmlDoc     As New HTMLDocument
    Dim htmlEle     As HTMLHtmlElement

    htmlDoc.body.innerHTML = HTMLCode
    For Each htmlEle In htmlDoc.All.tags("A")
        htmlEle.href = Replace$(htmlEle.href, "about:blank", "")
    For Each htmlEle In htmlDoc.All.tags("IMG")
        htmlEle.src = Replace$(htmlEle.href, "about:blank", "")
    Set GetHTMLDocument = htmlDoc
End Function
LVL 28

Expert Comment

ID: 6989921
Sorry, second For...Next loop should say .src on both sides of the equation.

Author Comment

ID: 6990244
OK, thanx!
However I didn't mean what you last suggested.
When I load a file in a webbrowser control, for
i get something like
Using your way, I get
If I remove about:blank, I'll still get the relative path to the image, but I need  C:\Test\Img.jpg
Replacing about:blank with the html file path will not work in all cases(like for images above the html file folder)
What I needed is to create htmlDoc from a file not from a string, in order to get the same results I get from a file loaded in a webbrowser control
LVL 28

Expert Comment

ID: 6990668
Try it out, and let me know what happens.  Since image sources can be relative paths, I imagine that will still remain the same even if you load it from a file and remove the about:blank portion.  For example, an html file on my machine my say:

<img src="images/mypic.jpg">

So even if you load as a string, the about:blank will appear before that, but you should be able to reconstruct the full path to that image, if you need it, since you have the path to the actual html file itself, and now, the relative path to the image.  So in our example case, if the full path to the html file to open was:


Then you know the image above must be located in:


Be sure to do the Replace function for all elements that can have a source or href type tag.  Some that come to mind are:

Javascript files
Stylesheet files

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 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