# read from XML file into a Word Document table

I have an xml file ( C:\Emp.Xml) with employee information in it
It has data with empoyee_id , employee_name, location and salary information
These are the columns and the data is in xml format
Emp_id, Emp_name, Loc, Sal

The number of records in the xml file is varying

I would like to read the xml file and populate the value into a table in a word document
The table row size is dynamically changing based on the number of records in the XML file
Could some one please suggest a VBA macro as to how I can populate XML data into a Word document table
###### 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.

RetiredCommented:
Can you parse the XLM file, but can't do the table-filling part?
0
Author Commented:
yes.. i do not know how do the table filling part...could you please suggest
0
RetiredCommented:
If you can provide each row's data as an array, this code will fill the first table in the active document.

It does expect a table to be there, with the correct number of columns.

Sub FillTable(strData() As String)
Dim tbl As Table
Dim c As Integer
Dim rw As Row

Set tbl = ActiveDocument.Tables(1)
For c = 0 To UBound(strData)
rw.Cells(c + 1).Range.Text = strData(c)
Next c
End Sub

0
Author Commented:
hi.. what is strdata()... how do it get or read strdata ?
0
Author Commented:
Graham.. I am sorry when you asked me ... "Can you parse XML file ".. I think I misunderstood you....
Using MS Word VBA , I cannot read an XML file , parse it and build a parse string with that .
I am sorry for the trouble
0
RetiredCommented:
We will have to research that bit, because I'm not sure about it. Can you attach a bit of it with disguised names (if they are real data).
0
Author Commented:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<Employees>

<Employee>
<Empid>101</Empid>
<Empname>Bob Dylan</Empname>
<Location>USA</Location>
<Salary>2000</Salary>
<Deptno>10</Deptno>
</Employee>

<Employee>
<Empid>102</Empid>
<Empname>Dean Jones</Empname>
<Location>UK</Location>
<Salary>3000</Salary>
<Deptno>20</Deptno>
</Employee>

<Employee>
<Empid>103</Empid>
<Empname>Brenda Hardy</Empname>
<Location>Italy</Location>
<Salary>4500</Salary>
<Deptno>30</Deptno>
</Employee>

.
.
.
.
</Employees>
0
Author Commented:
This is how I would like to have the output in the MS word table

101      Bob Dylan              USA      2000      10
102      Dean Jones              UK      3000      20
103      Brenda Hard         Italy      4500      30

Could you please suggest a MS Word VBA code for this

thanks
0
RetiredCommented:
If you add a reference to the Microsoft XML library, you might find the job easier, but you have to know how to use it, so I have use old-fashioned methods here.
Sub SimpleParseXML()
Dim f As Integer
Dim strTextData()  As String
Dim strTextLine As String
Dim strNames(4) As String

f = FreeFile
Open "C:\My Documents\Allwork\ee\23145871\employees.xml" For Input As #f
Do
Line Input #f, strTextLine
Select Case True
Case InStr(strTextLine, "<Employee>")
ReDim strTextData(4)
Case InStr(strTextLine, "</Employee>")
FillTable strTextData() 'Fill the Word table
Case InStr(strTextLine, "<Empid>")
strTextData(0) = Split(Split(strTextLine, "<Empid>")(1), "</Empid>")(0)
Case InStr(strTextLine, "<Empname>")
strTextData(1) = Split(Split(strTextLine, "<Empname>")(1), "</Empname>")(0)
Case InStr(strTextLine, "<Location>")
strTextData(2) = Split(Split(strTextLine, "<Location>")(1), "</Location>")(0)
Case InStr(strTextLine, "<Salary>")
strTextData(3) = Split(Split(strTextLine, "<Salary>")(1), "</Salary>")(0)
Case InStr(strTextLine, "<Deptno>")
strTextData(4) = Split(Split(strTextLine, "<Deptno>")(1), "</Deptno>")(0)
End Select
Loop Until EOF(f)
Close #f
End Sub

0

Experts Exchange Solution brought to you by