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!

Extract XML Data in Word using VBA

Status
Not open for further replies.

BitNet33

Technical User
Feb 2, 2011
21
CA
Hey, so I'm new to VBA and need some help coding. I am trying to use Microsoft Word to open .xml files with data in them and use VBA to extract the data from the fields that I need. For example, when I open the XML files in Word, I would have:
<information>
<client>
<name>Jim</name>
</client>
<client>
<name>Bob</name>
</client>
</information>

and I want to write code that will go through the file, find the names of the clients (Jim and Bob), and save them to a seperate location (like a text file). Right now I have code that doesn't work very well (I'm trying to learn, I've just sort of copied/pasted stuff) and it only grabs one name, but I need it to go through and grab all of the possible names. Here is my current (flawed) code:
Public Sub FindText()

Dim StartWord As String, EndWord As String
StartWord = "<name>"
EndWord = "</name>"

With ActiveDocument.Content.Duplicate
.Find.Execute FindText:=StartWord & "*" & EndWord, MatchWildcards:=True
.MoveStart wdCharacter, Len(StartWord)
.MoveEnd wdCharacter, -Len(EndWord) - 1
.Font.Bold = True
.Select
Selection.Copy
End With

Dim name As String
name = Selection

Open "C:\Desktop\Names.txt" For Output As #1
Write #1, name
Close #1
End Sub

Any help would be great! Thanks in advance!
-BitNet33
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top