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.

Can you parse the XLM file, but can't do the table-filling part?
vsuripeddiAuthor Commented:
yes.. i do not know how do the table filling part...could you please suggest
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)
    Set rw = tbl.Rows.Add
    For c = 0 To UBound(strData)
        rw.Cells(c + 1).Range.Text = strData(c)
    Next c
End Sub

Open in new window

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

vsuripeddiAuthor Commented:
hi.. what is strdata()... how do it get or read strdata ?
vsuripeddiAuthor 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 .
Could you please suggest
I am sorry for the trouble
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).
vsuripeddiAuthor Commented:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>

    <Empname>Bob Dylan</Empname>
    <Empname>Dean Jones</Empname>
    <Empname>Brenda Hardy</Empname>
vsuripeddiAuthor 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

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vsuripeddiAuthor Commented:

Thanks a lot for your solution. That was really valuable
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.