Results 1 to 11 of 11

Thread: VB6 upgrade problem MSHTML COM with VB.NET [resolved]

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    VB6 upgrade problem MSHTML COM with VB.NET [resolved]

    I have this code from VB6 that works fine
    VB Code:
    1. Dim objLink As HTMLLinkElement
    2.     Dim objMSHTML As New MSHTML.HTMLDocument
    3.     Dim objDoc As MSHTML.HTMLDocument
    4.    
    5.     Set objDoc = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
    6.    
    7.     While objDoc.readyState <> "complete"
    8.         DoEvents
    9.     Wend
    10.     'get all Links
    11.     For Each objLink In objDoc.links
    12.         Debug.Print objLink
    13.     Next
    I then run it through the VS.Net VB upgrade tool and get this
    VB Code:
    1. Dim objLink As mshtml.HTMLLinkElement
    2.         Dim objMSHTML As New mshtml.HTMLDocument
    3.         Dim objDoc As mshtml.HTMLDocument
    4.  
    5.         objDoc = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
    6.  
    7.         While objDoc.readyState <> "complete"
    8.             System.Windows.Forms.Application.DoEvents()
    9.         End While
    10.         'get all Links
    11.         For Each objLink In objDoc.links
    12.             System.Diagnostics.Debug.WriteLine(objLink)
    13.         Next objLink
    I get an error on the line
    VB Code:
    1. 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
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    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.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  5. #5

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    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.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.

  7. #7

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    Re: VB6 upgrade problem

    Half the properties of the object objMSHTML have the Null Reference
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.

  9. #9

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    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:
    1. Dim objMSHTML As New mshtml.HTMLDocument
    2.     Dim objDocument As mshtml.IHTMLDocument2
    3.     Dim ips As IPersistStreamInit
    4.     Dim HTMLTagCollection As mshtml.IHTMLElementCollection
    5.     Dim item As mshtml.IHTMLAnchorElement
    6.     ips = DirectCast(objMSHTML, IPersistStreamInit)
    7.     ips.InitNew()
    8.     objDocument = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
    9.     Do Until objDocument.readyState = "complete"
    10.       Application.DoEvents()
    11.     Loop
    12.     TextBox1.Text = objDocument.body.innerHTML()
    13.     Dim ObjDocument3 As mshtml.IHTMLDocument3
    14.     ObjDocument3 = DirectCast(objDocument, mshtml.IHTMLDocument3)
    15.     HTMLTagCollection = ObjDocument3.getElementsByTagName("A")
    16.     For Each item In HTMLTagCollection
    17.       MsgBox(item.href)
    18.     Next
    But I am getting Type 'IPersistStreamInit' is not defined. Does anyone know what I need to reference to use IPersistStreamInit?
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.

  11. #11

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    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:
    1. 'First, include the InteropServices namespace
    2. Imports System.Runtime.InteropServices
    Then
    VB Code:
    1. Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    2.     Dim objMSHTML As New mshtml.HTMLDocument
    3.         Dim objDocument As mshtml.IHTMLDocument2
    4.         Dim ips As IPersistStreamInit
    5.         Dim HTMLTagCollection As mshtml.IHTMLElementCollection
    6.         Dim item As mshtml.IHTMLAnchorElement
    7.         ips = DirectCast(objMSHTML, IPersistStreamInit)
    8.         ips.InitNew()
    9.         objDocument = objMSHTML.createDocumentFromUrl("http://www.yahoo.com", vbNullString)
    10.         Do Until objDocument.readyState = "complete"
    11.             Application.DoEvents()
    12.         Loop
    13.         TextBox1.Text = objDocument.body.innerHTML()
    14.         Dim ObjDocument3 As mshtml.IHTMLDocument3
    15.         ObjDocument3 = DirectCast(objDocument, mshtml.IHTMLDocument3)
    16.         HTMLTagCollection = ObjDocument3.getElementsByTagName("A")
    17.         For Each item In HTMLTagCollection
    18.             MsgBox(item.href)
    19.         Next
    20.     End Sub
    21.  
    22.     Public Enum HRESULT
    23.         S_OK = 0
    24.         S_FALSE = 1
    25.         E_NOTIMPL = &H80004001
    26.         E_INVALIDARG = &H80070057
    27.         E_NOINTERFACE = &H80004002
    28.         E_FAIL = &H80004005
    29.         E_UNEXPECTED = &H8000FFFF
    30.     End Enum
    31.  
    32.     'IPersistStreamInit interface
    33.     <ComVisible(True), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), _
    34.     InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
    35.     Public Interface IPersistStreamInit : Inherits IPersist
    36.  
    37.         Shadows Sub GetClassID(ByRef pClassID As Guid)
    38.         <PreserveSig()> Function IsDirty() As Integer
    39.         <PreserveSig()> Function Load(ByVal pstm As UCOMIStream) As HRESULT
    40.         <PreserveSig()> Function Save(ByVal pstm As UCOMIStream, <MarshalAs(UnmanagedType.Bool)> ByVal fClearDirty As Boolean) As HRESULT
    41.         <PreserveSig()> Function GetSizeMax(<InAttribute(), Out(), _
    42.         MarshalAs(UnmanagedType.U8)> ByRef pcbSize As Long) As HRESULT
    43.         <PreserveSig()> Function InitNew() As HRESULT
    44.  
    45.     End Interface
    46.  
    47.     ' IPersist interface
    48.     <ComVisible(True), ComImport(), Guid("0000010c-0000-0000-C000-000000000046"), _
    49.     InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
    50.     Public Interface IPersist
    51.         Sub GetClassID(ByRef pClassID As Guid)
    52.     End Interface
    Last edited by blindlizard; Jul 9th, 2005 at 04:53 PM.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

Posting Permissions

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



Click Here to Expand Forum to Full Width