Matheno
Matheno

Reputation: 4142

VisualBasic CreateObject()

I'm having a problem with CreateObject. It keeps telling me it's an undeclared element. Is there some specific reference I need to add?

The reference for excel etc are already in it, and don't get any more info from MSDN.

Public Function BBANtoBIC(ByVal BBAN As String) As String
        Dim xd As Object
        xd = CreateObject("msxml2.domdocument.4.0")
        xd.Load("http://www.ibanbic.be/IBANBIC.asmx/BBANtoBIC?value=" & BBAN)
        Do While xd.readyState <> 4
            DoEvents()
        Loop
        BBANtoBIC = xd.Text
    End Function

Upvotes: 0

Views: 447

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Reading through that function, all it does is return the text of the document. It doesn't really do anything with the xml. Therefore, you could replace it with this:

Public Function BBANtoBIC(ByVal BBAN As String) As String
    Using wc As New System.Net.WebClient()
        Return wc.DownloadString("http://www.ibanbic.be/IBANBIC.asmx/BBANtoBIC?value=" & BBAN)
    End Using
End Function

I see only two reasons why this might not be true. The first is if you're counting the on xml document object to ensure that what you download really is valid, well-formed xml, in which case there should be error handling code near where the function is called. The second is if the DoEvents wait-loop is there for program responsiveness rather than waiting on the xml parser... which is also very poor design. Using DoEvents() in that way can have side effects. If it's only about waiting on the xml parser, the new code should avoid that delay entirely.

If you do need to parse/validate the xml, there are at least three different ways built-into the .Net framework to do this without needing to rely on CreateObject(). There should be no need to use CreateObject() in .Net. Ever

Upvotes: 1

IvanH
IvanH

Reputation: 5139

"msxml2.domdocument.4.0" is probably not installed in your system. It it from 2001. You shoud probably use "Msxml2.DOMDocument.6.0". See Using the right version of MSXML in Internet Explorer

Upvotes: 2

Related Questions