loading a url using mshtml.createDocumentFromUrl

G

Guest

Hello,
I am having certain problems in trying to use the function
createDocumentFromUrl in VB.NET but get this error.."Object reference not set
to an instance of object".
Here is the code that I am using

Imports mshtml

Public DocumentFactory As HTMLDocument
Public Function GetWebPage(ByVal strURL As String) As HTMLDocument
Try
GetWebPage = DocumentFactory.createDocumentFromUrl(strURL, vbNullString)
StatusBar1.Panels(1).Text = strURL
Do Until GetWebPage.readyState = "complete"
Application.DoEvents()
Loop
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function

I was reading through the similar situation but the gentleman posting the
reply has submiited a attachment which is not be found anymore. I would
greatly apprecitate if the code is posted again.

Thanking you in advance,

Vibhu Bansal.
 
G

Guest

Further to my post, I need to use IPersistStreamInit. I was reading this in
this forum itself. But unfortunately, the solution file posted as an
attachment is missing. I would request anybody who has a clue to it to get
asap.
Vibhu Bansal.
 
G

Guest

Hi Charles,
It seems that the attachment is not getting posted. Please email the
attachment to me at (e-mail address removed) if you can.
BTW, the gentleman is you itself and I was reading the post sometime of late
last year within this forum.
Vibhu.
 
C

Cor Ligthert

Vibhu,

It is in the newsgroup, what you can try is to attach this newsgroup direct
on the microsoft.newsserver
news://news.microsoft.com/microsoft.public.dotnet.languages.vb

As well to inform Charles from that.

Cor
 
C

Charles Law

I have just sent it so you should receive it shortly.

I am able to see the attachment on the newsgroup using MS Outlook Express as
my newsreader, so perhaps it is something at your end that is preventing you
from seeing it.

Charles
 
G

Guest

Hi Charles,
Thanks for the info. Now I am getting stuck up in the next part i.e. using
this fuction, I need to get all elements by their tag names. So in VB6, I was
using the function getElementsbyTagname. But now as the page is being
retrieved in IHTMLDocument2 object, it does not support this function. Can
you provide me hints as to how I can use this functionality. I need to use
this function to look for images and store them on the local folder in the
harddisk.
Any help or references would be greatly beneficial.
Thanking You,
Vibhu.
 
C

Charles Law

Vibhu

getElementsByTagName is defined on the IHTMLDocument3 interface. Use
DirectCast to get this interface from the document object and you will then
be able to call the method as before.

HTH

Charles
 
G

Guest

Hi,
Instead of using getElementsbytagname, within IHTMDocument2 I stumbled on a
function of anchors. This is what the code looks like now...

Private Sub SearchUrl(ByVal strURL As String)
Dim objMSHTML As New mshtml.HTMLDocument
Dim objDocument As mshtml.IHTMLDocument2
Dim ips As IPersistStreamInit
Dim item As mshtml.IHTMLElement
ips = DirectCast(objMSHTML, IPersistStreamInit)
ips.InitNew()
objDocument = objMSHTML.createDocumentFromUrl(strURL, vbNullString)
Do Until objDocument.readyState = "complete"
Application.DoEvents()
Loop
For Each item In objDocument.anchors
Dim strUrl1 As mshtml.IHTMLAnchorElement
strUrl1 = item
MsgBox(strUrl1.href)
Next
End Sub
Now when this function is called, it does loop through objDocument.anchors
but shows a message box without any details. This is happening because it
seems that
objDocument's state is still not complete. I reached this inference because
the text in the text box is displayed after the message box has clicked on.

Any clues on why this is happening.

Vibhu.
 
G

Guest

Hi,
I finally got the problem working. Here is the solution...

We do need use IHTMLDocument3 in order to get the links with a page that has
been loaded.

Thanks Charles for the help you provided.

Private Sub SearchUrl(ByVal strURL As String)
Dim objMSHTML As New mshtml.HTMLDocument
Dim objDocument As mshtml.IHTMLDocument2
Dim ips As IPersistStreamInit
Dim HTMLTagCollection As mshtml.IHTMLElementCollection
Dim item As mshtml.IHTMLAnchorElement
ips = DirectCast(objMSHTML, IPersistStreamInit)
ips.InitNew()
objDocument = objMSHTML.createDocumentFromUrl(strURL, vbNullString)
Do Until objDocument.readyState = "complete"
Application.DoEvents()
Loop
TextBox1.Text = objDocument.body.innerHTML()
Dim ObjDocument3 As mshtml.IHTMLDocument3
ObjDocument3 = DirectCast(objDocument, mshtml.IHTMLDocument3)
HTMLTagCollection = ObjDocument3.getElementsByTagName("A")
For Each item In HTMLTagCollection
MsgBox(item.href)
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top