• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 764
  • Last Modified:

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
  • 6
  • 4
1 Solution
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

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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

vsuripeddiAuthor Commented:

Thanks a lot for your solution. That was really valuable

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now