XML Data Extraction

H

Hafeez Esmail

This is probably not the best way of doing this....but it's the only thing I
could come up with in an hour...

I have an XML file that contains data I'd like extracted, and placed in
Excel (or Access, or a CSV file....whatever's simpliest). Here's what I did.

I changed the extension from .xml to .doc
I wrote the following script (truncated...showing main part only) to
essential exploit the Selection.Find.Execute command to look for the tags I
want, but for some reason this code requires me to make a highlighted
selection at the beginning of the document.

Dim blExit As Boolean 'Exit condition
blExit = False
Do Until blExit
'Get all the data
For ctr = LBound(arystrTags) To UBound(arystrTags) 'Multiple Tags defined
Selection.Find.Text = "\<" & arystrTags(ctr) & "\>*\</" &
arystrTags(ctr) & "\>"
blExit = Not Selection.Find.Execute() 'This is where it is messing
up...
'Gets useful part of data
strXML = Selection.Text
intStart = InStr(1, strXML, ">") + 1
intEnd = InStr(1, strXML, "/") - 1
If ctr = UBound(arystrTags) Then
strData = strData & Mid(strXML, intStart, intEnd - intStart)
Else
strData = strData & Mid(strXML, intStart, intEnd - intStart) & ","
End If
Next
'Write to CSV file
If Not blExit Then
tsCSV.WriteLine strData 'TextStream Object
End If
strData = ""
Loop

If I open the word file (containing the code and the XML Data), and run the
macro (from the Visual Basic Editor screen) without moving the cursor or
I'd like some help on any of the following matters:
1) Why does it require me to make a highlited selection at the begining of
the doucment
2) I'd like to modify this code to explicitly reference all objects...so
that it can be run from an external application, if necessary
3) The proper way of how I should be going about doing this.

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top