Reading XML files from MS Word 2010 VBA

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

Kind regards,

Joop
ocmcnlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steven HarrisPresidentCommented:
[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.
0
ocmcnlAuthor Commented:
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
0
Steven HarrisPresidentCommented:
I am not aware of any modules or add-ins with that type of functionality for XML; however, that doesn't mean they don't exist.

The best thing I could do is point you towards the MSDN site:

Putting XML to Work

A Beginner's Guide to the XML DOM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ocmcnlAuthor Commented:
Thank you so much!! I will study the sites you linked to.
0
ocmcnlAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.