Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting XML Tags in Word with VBA

Status
Not open for further replies.

BitNet33

Technical User
Feb 2, 2011
21
CA
I am opening XML files in Word and am trying to extract certain fields from the huge XML file so that I only have the portions of it that I need. When I open the XML file in word it sees the fields (ex "<customer>" and "</customer") as a "tag". How can I use VBA to reference those tags so that I can only grab the information from between the tags that I want?
Thanks for any help!

BitNet
 
The fact they are "tags" is utterly irrelevant. Word does not recognize them as tags. So. You want to extract text between two different seatch strings.

"<customer>" text text "</customer>"

or

"this is serach string 1" text text "The quick red fox blah"

Makes no difference to Word. Two different search strings, and the text between them.

So, say, you have:

<customer>yadda blah </customer>
<customer>whatever </customer>

<customer>ho hum </customer>

shdkhskdhs asdlahd adh

Code:
Sub Blah()
Dim MyTags()
Dim r As Range
Dim r2 As Range
Dim r3 As Range
Dim msg As String
Set r = ActiveDocument.Range
MyTags() = Array("<customer>", "</customer>")
With r.Find
   .ClearFormatting
   Do While .Execute(Findtext:=MyTags(0), Forward:=True) = True
      Set r2 = ActiveDocument.Range( _
         Start:=r.End, End:=ActiveDocument.Range.End)
      With r2.Find
         .ClearFormatting
         .Text = MyTags(1)
         .Execute
         Set r3 = ActiveDocument.Range( _
            Start:=r.End, End:=r2.Start - 1)
         msg = msg & r3.Text & vbCrLf
      End With
   Loop
End With
MsgBox msg
End Sub
returns (using the MsgBox msg):

yadda blah
whatever
ho hum

So. You can get th0e text between two search strings, but this may, or may not, help. You can not, at least with the above code, reference them.

BTW: it is easy to make the two search strings variable from a user input. So you could put in <whatever> and </whatever>, or <client> and </client>.

247.50 hours to retirement
 
Ok so I've decided to use XML DOM, which I think is what Tony was talking about. So I now have this code:
Code:
Sub FindNode()

Dim xmlDoc As DOMDocument
Dim xmlNode As IXMLDOMNode
Set xmlDoc = New DOMDocument
xmlDoc.async = False
xmlDoc.Load ("C:\Documents and Settings\User\Desktop\Customer.xml")
Set xmlNode = xmlDoc.SelectSingleNode("/name")
MsgBox xmlNode

End Sub
But when I run this, I get "Run-time error '91': Object variable or With block variable not set". And its talking about my variable xmlNode. Why isn't it being set? Shouldn't this grab the "name" node? Any ideas?
 
Ah. Moved into an area where I am totally ignorant. I am sure Tony will be able to help you.

55,687.00 hours down....
157 hours to go
 
Not exactly, but it will work.

Your xmlNode variable is 'Nothing' because you don't have an element matching the selection criteria - perhaps you need something like "customer/name" - I don't know because I can't see your xml.


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Thanks for your help fumei. I'm pretty ignorant in this area too haha, hence all the problems I'm having.

Tony, I thought that that might be the problem as well, so I tried playing around with the "customer/name" portion. I'm not sure of exactly the format it needs. Do you need to include every "parent" node along the way in which the "child" node is inside of? Cause I've tried pretty well every combination of the nodes and I always get the same error message "Object variable not set". Say I had this in my XML:
Code:
<componentOf>
  <timeEvent>
    <customerList>
      <customer>
        <name> Bob </name>
        <id> 12345 </id>
      </customer>
    </customerList>
  </timeEvent>
</componentOf>
and my VBA code looks like this:
Code:
Sub FindNode()

Dim xmlDoc As MSXML2.DOMDocument
Dim xmlNode As IXMLDOMNode
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.Load ("C:Documents and Settings\User\DesktopCustomer.xml")
Set xmlNode = xmlDoc.FirstChild
Debug.Print xmlDoc.SelectSingleNode("name").text

End Sub
In "selectSingleNode()" should I have "name", or "customer/name", or "componentOf/timeEvent/customerList/customer/name"?

This problem is making me crazy! Thanks for any help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top