|
-
Jul 9th, 2005, 12:30 AM
#1
Thread Starter
Frenzied Member
VB6 upgrade problem MSHTML COM with VB.NET [resolved]
I have this code from VB6 that works fine
VB Code:
Dim objLink As HTMLLinkElement
Dim objMSHTML As New MSHTML.HTMLDocument
Dim objDoc As MSHTML.HTMLDocument
Set objDoc = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
While objDoc.readyState <> "complete"
DoEvents
Wend
'get all Links
For Each objLink In objDoc.links
Debug.Print objLink
Next
I then run it through the VS.Net VB upgrade tool and get this
VB Code:
Dim objLink As mshtml.HTMLLinkElement
Dim objMSHTML As New mshtml.HTMLDocument
Dim objDoc As mshtml.HTMLDocument
objDoc = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
While objDoc.readyState <> "complete"
System.Windows.Forms.Application.DoEvents()
End While
'get all Links
For Each objLink In objDoc.links
System.Diagnostics.Debug.WriteLine(objLink)
Next objLink
I get an error on the line
VB Code:
objDoc = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
Saying "An unhandled exception of type 'System.NullReferenceException' occured in mscorlib.dll"
"Additional Information: Object reference not set to an instance of an object"
Last edited by blindlizard; Jul 9th, 2005 at 04:54 PM.
Reason: Resolved
-
Jul 9th, 2005, 12:40 AM
#2
Re: VB6 upgrade problem
That tool is useless, except if you are having trouble with something, and have no idea how to do it, you plug a few lines into a vb project, and then upgrade that. My first app got upgraded perfectly, so I sent it along, but found it didn't run anywhere else (Hello, World). The second one (draw clockface) failed also. It got upgraded, but didn't run anywhere else.
Even their upgrade sample (in 101 Samples). I tried to open the upgrade project, but had a problem with the ADODC component. Finally, I opened the project vbp file, and upgraded it. It worked, and I looked in the components and found that it referenced a folder placed on my c:drive called Interop. I added that reference to the upgrade app and it worked.
If they had a way to re-write 100,000 lines of code, they would have done it, but then nobody would have to learn Net. People would develop with VB6 and update it to run with Net.
I'd like it, but I'm learning Net to be able to update my own apps.
-
Jul 9th, 2005, 01:00 AM
#3
Thread Starter
Frenzied Member
Re: VB6 upgrade problem
Well, it looks ok, it looks just like the VB6 code...I just wonder why it won't work, and how to fix it.
-
Jul 9th, 2005, 01:22 AM
#4
Re: VB6 upgrade problem
I just tried to run the Web sample, and got a UDDI error, which I can't figure out. I'll have to wait until tomorrow. I'm not even sure it it's what you wanted.
-
Jul 9th, 2005, 01:26 AM
#5
Thread Starter
Frenzied Member
Re: VB6 upgrade problem
I just want to fix my code. I can't figure out why I am getting an error in the VB.Net code because it looks right.
-
Jul 9th, 2005, 04:18 AM
#6
Re: VB6 upgrade problem
It doesn't matter what it looks like. Put a break point on that line and run it. When execution stops at that line examine everything to see what variable is the null reference. Then you can trace that reference's history to see where it should have been assigned an object.
-
Jul 9th, 2005, 04:32 AM
#7
Thread Starter
Frenzied Member
Re: VB6 upgrade problem
Half the properties of the object objMSHTML have the Null Reference
-
Jul 9th, 2005, 04:45 AM
#8
Re: VB6 upgrade problem
Sorry. Posted hastily and didn't notice that the null reference exception actually occurred in mscorlib. I'm afraid I'm unfamiliar with the classes you're using so I can't be of further help, although I haven't been of help up to now either. I searched MSDN for "createdocumentfromurl" and didn't get anything that related to the mshtml class.
-
Jul 9th, 2005, 08:26 AM
#9
Thread Starter
Frenzied Member
Re: VB6 upgrade problem
I found this which should do what I am trying to do (which is get all the links from a HTML page)
VB Code:
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("http://www.yahoo.com", 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
But I am getting Type 'IPersistStreamInit' is not defined. Does anyone know what I need to reference to use IPersistStreamInit?
-
Jul 9th, 2005, 10:11 AM
#10
Re: VB6 upgrade problem
You would presumably have to import the namespace to which the interface belongs, or else qualify the name in your code with the namespace. When you are posting code here that includes classes from non-standard namespaces, it is a good idea to qualify them anyway because the rest of us may not know what they are otherwise.
-
Jul 9th, 2005, 04:30 PM
#11
Thread Starter
Frenzied Member
Re: VB6 upgrade problem
Yeah I get that, but I don't know what namespace 'IPersistStreamInit' is in. It is a COM thing I think
**edit**
Ok, I got it. IPersistStreamInt is a COM thing (I don't quite understand it) that basically gets the handle of the object so that the COM object we are calling knows what is going on. IPersistStreamInit is not an interface/function included in any interface, you have to define it.
VB Code:
'First, include the InteropServices namespace
Imports System.Runtime.InteropServices
Then
VB Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
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("http://www.yahoo.com", 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
Public Enum HRESULT
S_OK = 0
S_FALSE = 1
E_NOTIMPL = &H80004001
E_INVALIDARG = &H80070057
E_NOINTERFACE = &H80004002
E_FAIL = &H80004005
E_UNEXPECTED = &H8000FFFF
End Enum
'IPersistStreamInit interface
<ComVisible(True), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IPersistStreamInit : Inherits IPersist
Shadows Sub GetClassID(ByRef pClassID As Guid)
<PreserveSig()> Function IsDirty() As Integer
<PreserveSig()> Function Load(ByVal pstm As UCOMIStream) As HRESULT
<PreserveSig()> Function Save(ByVal pstm As UCOMIStream, <MarshalAs(UnmanagedType.Bool)> ByVal fClearDirty As Boolean) As HRESULT
<PreserveSig()> Function GetSizeMax(<InAttribute(), Out(), _
MarshalAs(UnmanagedType.U8)> ByRef pcbSize As Long) As HRESULT
<PreserveSig()> Function InitNew() As HRESULT
End Interface
' IPersist interface
<ComVisible(True), ComImport(), Guid("0000010c-0000-0000-C000-000000000046"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IPersist
Sub GetClassID(ByRef pClassID As Guid)
End Interface
Last edited by blindlizard; Jul 9th, 2005 at 04:53 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|