Question: How do I use Visual Basic to access the HTML code of a website?
Processing a web page in a VB program isn't a problem. In VB6, you can do it by using InternetExplorer object. The following example will give you an idea about how to get started.
Set TheBrowser = _ CreateObject("InternetExplorer.Application") TheBrowser.Visible = True ' Note: You can also process an HTML ' file offline this way. ' Just use the path to the file ' instead of the web address. TheBrowser.Navigate _ URL:="http://visualbasic.about.com" 'set the browser in the top TheBrowser.Top = 100 'left of the user's screen TheBrowser.Left = 100 'display the status bar TheBrowser.StatusBar = True 'open the default Search page TheBrowser.GoSearch 'open the default Home page TheBrowser.GoHome 'refresh the current page TheBrowser.Refresh 'close the browser TheBrowser.Quit
Although this works in IE 7, Microsoft has made things more difficult with Internet Explorer 8 because this version has more security built in. According to Microsoft:
Internet Explorer 8. On Windows Vista, to create an instance of Internet Explorer running at a medium integrity level, pass CLSID_InternetExplorerMedium (defined in exdisp.idl) to CoCreateInstance. The resulting InternetExplorerMedium object supports the same events, methods, and properties as the InternetExplorer object.
The problem with this cryptic note is that CoCreateInstance is a OLE API call (CreateObject is the VB6 analog) and there is no clue there as to how to do this in VB6. (I can't help saying it. "Typical Microsoft documentation.") As of this writing, IE 8 is only a few weeks old and I haven't been able to crack this problem.
An alternative way to do this is to use a lower-level control called the Internet Transfer Control. This object implements HTTP and FTP (the File Transfer Protocol) and lets you can connect to a site and retrieve files. This is a "lower level" approach since a web site will be retrieved as text rather than through method calls as with the InternetExplorer object. But the Internet Transfer Control avoids the overhead of the browser and lets you get at the HTML directly. In VB6, you can add the component using the Components ... menu item on the Project menu.
Web Pages Using VB.NET
In VB.NET, you can add a reference to ShDocVw.dll (in Windows/System32) using the Browse tab in Add Reference) and open an instance of IE 8 using the new "InPrivate" mode.
Dim TheBrowser = New SHDocVw.InternetExplorerMedium TheBrowser.Visible = True TheBrowser.Navigate( _ URL:="http://visualbasic.about.com")
But in VB.NET, you don't even have to do that. The problem can be solved much more simply. Just add a WebBrowser control to your project. For example, this one line of code accesses an HTML file on my local computer.