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
<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