3

Greetings for the day,

Hi, I am a beginner using vb 6.0. I am using the following code and getting 'user defined type not defined'.the code is below.the line where i get error is highlighted.Kindly help.should i add some references or components?if so,what it would be. your timely and kindly help will be much more helpful for me

Public Sub LoadDocument() 
    Dim xDoc As MSXML2.DOMDocument
    Set xDoc = New MSXML2.DOMDocument 
    xDoc.async = False 
    xDoc.validateOnParse = False
    If xDoc.Load("C:\Users\284582\Desktop\XML1.xml") Then
            DisplayNode xDoc.ChildNodes, 0 
    End If 
End Sub    

' Error on this line'
Public Sub DisplayNode(ByRef Nodes As MSXML.IXMLDOMNodeList, _
           ByVal Indent As Integer)    

    Dim xNode As MSXML.IXMLDOMNode
    Indent = Indent + 2    
    For Each xNode In Nodes
        If xNode.NodeType = NODE_TEXT Then

            Debug.Print Space$(Indent) & xNode.ParentNode.nodeName & _
                ":" & xNode.NodeValue 
        End If    

        If xNode.HasChildNodes Then   
            DisplayNode xNode.ChildNodes, Indent  
        End If    
    Next xNode   
End sub    
Steve
  • 213,761
  • 22
  • 232
  • 286
user1724956
  • 75
  • 3
  • 3
  • 7

2 Answers2

3

It's MSXML2.IXMLDOMNodeList, not MSXML.IXMLDOMNodeList.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 2
    Also he probably should add `Option Explicit` or better yet turn on the IDE Option to cause it to be autogenerated when creating modules. – Bob77 Dec 08 '12 at 15:50
1

The library may be missing from your references. Try this.

Manually adding MSXML2

1. Open MS Access.
2. Database Tools ribbon
3. Visual Basic  ribbon item (icon)
4. Double-click on any module to open it.

5. Tools menu
6. References…
7. Find  Microsoft XML, v6.0. is in the list
    a. If in list but not checked, check it and click [OK].
    b. If not in the list: 
        i. click [Browse…] and add "c:\windows\system32\msxml6.dll"
8. [OK] your way back to the Visual Basic window.
9. Close the Visual Basic Window.  You should be good to go.

Programmatically adding MSXML2 Add the following sub and function. Run the sub. Edit the sub to include a path if necessary.

Check for broken references in the library Adapted from Add references programatically

Sub CheckXmlLibrary()
' This refers to your VBA project.
    Dim chkRef As Reference, RetVal As Integer  ' A reference.
    Dim foundWord As Boolean, foundExcel As Boolean, foundXml As Boolean
    foundWord = False
foundExcel = False
    foundXml = False

' Check through the selected references in the References dialog box.
    For Each chkRef In References
' If the reference is broken, send the name to the Immediate Window.
        If chkRef.IsBroken Then
           Debug.Print chkRef.Name
        End If
'copy and repeat the next 2 if statements as needed for additional libraries.
    If InStr(UCase(chkRef.FullPath), UCase("msxml6.dll")) <> 0 Then
                foundXml = True
            End If
    Next

    If (foundXml = False) Then
            'References.AddFromFile ("C:\Windows\System32\msxml6.dll")  <-- For other than XML, modify this line and comment out line below.
            RetVal = AddMsXmlLibrary
            If RetVal = 0 Then MsgBox "Failed to load XML Library (msxml6.dll).  XML upload/download will not work."
    End If

End Sub

Add XML reference to the library Developed by Chris Advena. Thanks to http://allenbrowne.com/ser-38.html for the insight.

Public Function AddMsXmlLibrary(Optional PathFileExtStr As String = "C:\Windows\System32\msxml6.dll") As Integer
On Error GoTo FoundError
AddMsXmlLibrary = 1
References.AddFromFile (PathFileExtStr)

AllDone:
    Exit Function
FoundError:
    On Error Resume Next
    AddMsXmlLibrary = 0
    On Error GoTo 0
End Function
Community
  • 1
  • 1
cadvena
  • 1,063
  • 9
  • 17