Link to home
Start Free TrialLog in
Avatar of ocmcnl
ocmcnl

asked on

Reading XML files from MS Word 2010 VBA

Does anybody has a module with code to read XML files?

Kind regards,

Joop
Avatar of Steven Harris
Steven Harris
Flag of United States of America image

[Alt] + [F11] to Access VBA Console > Tools > References... > Scroll down to Microsoft XML and enable checkbox to add to VBA.

Once that is done, you can use VBA snippets like-

Load and Retrieve XML Contents

Sub Q_28253839_1()
    Dim xdoc As MSXML2.DOMDocument
    Set xdoc = New MSXML2.DOMDocument

    xmldoc.async = False
    If xmldoc.Load("C:\Users\sharris\Desktop\Test.xml") Then
        Debug.Print xmldoc.XML
        Debug.Print xmldoc.Text
    End If
End Sub

Open in new window


Retrieving Specific Information from Element Nodes

Sub Q_28253839_2() 
    Dim xmldoc As MSXML2.DOMDocument50 
    Dim xmlNodeList As MSXML2.IXMLDOMNodeList 
    Dim myNode As MSXML2.IXMLDOMNode 

    Set xmldoc = New MSXML2.DOMDocument50 
    xmldoc.async = False 
    xmldoc.Load ("C:\yourFile.xml") 
    Set xmlNodeList = xmldoc.selectNodes("//Name") 
    If Not (xmlNodeList Is Nothing) Then 
        For Each myNode In xmlNodeList 
            Debug.Print myNode.Text 
            If myNode.Text = "old Text" Then 
                myNode.Text = "new Text" 
                xmldoc.Save "C:\newFile.xml" 
            End If 
        Next myNode 
    End If 
    Set xmlDoc = Nothing 
End Sub 

Open in new window


You can also check out the following MSDN link.
Avatar of ocmcnl
ocmcnl

ASKER

Thank you for your reply. I appreciate it very much. I know almost nothing about XML but understand how important it is.

What you send me is the basis to figure out how it works.

I have written routines for reading ini-files i.e. get all keys of a certain section, get a specific value of a key or read a complete section in a two-dimensional array.

Do you know where I can find or buy routines to do similar things with XML?

Kind regards,

Joop
ASKER CERTIFIED SOLUTION
Avatar of Steven Harris
Steven Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocmcnl

ASKER

Thank you so much!! I will study the sites you linked to.
Avatar of ocmcnl

ASKER

Being pretty lazy myself ;-)  I would have preferred ready to use routines. The links provided by ThinkSpacesolutions are very helpful. It saves me considerable time to find good resources on this subject.