none
XML Table Field To Actual Table in MS Word RRS feed

  • Question

  • Hello,

    I've visited the forums before, but this is my first time posting, so I hope I'm doing it right.

    I'm working on a project in Microsoft Word where I use VBA codes to strip XMLs of their text and places them using plain text content controls. I currently have VBA coding written that take the table XML information and transform them into linearized tables. However, I am in need of VBA codes that will take the XML information and transform them into actual tables that adjust in size based on the number of columns/rows present in the XML. I'm not sure what to do to create a table and was hoping to get some tips to send me in the right direction of how to write this code.

    Thank you for your time.

    Current VB resources being used include:
    Visual Basics for Applications
    Microsoft Word 14.0 Object Library
    OLE Automation
    Microsoft XML, v3.0
    Microsoft Scripting Runtime

    I can share an example of the linearized codes if necessary to understand the type of project I'm working toward.

    Wednesday, June 15, 2016 7:06 PM

Answers

  • >>>owever, I am in need of VBA codes that will take the XML information and transform them into actual tables that adjust in size based on the number of columns/rows present in the XML. I'm not sure what to do to create a table and was hoping to get some tips to send me in the right direction of how to write this code.<<<

    According to your description, you could use MSXML to parse XML, you could refer to below links:

    MSXML

    A Beginner's Guide to the XML DOM

    and create table with VBA, you could refer to below code:
    Sub Demo()
    
      intNoOfRows = 5
    
      intNoOfColumns = 3
    
      Set objWord = CreateObject("Word.Application")
    
      objWord.Visible = True
    
      Set objDoc = objWord.Documents.Add
    
      Set objRange = objDoc.Range
    
      objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns
    
      Set objTable = objDoc.Tables(1)
    
      objTable.Borders.Enable = True
    
      For i = 1 To intNoOfRows
    
         For j = 1 To intNoOfColumns
    
         objTable.Cell(i, j).Range.Text = "Sumit_" & i & j
    
         Next
         
      Next
      
      Set objDoc = Nothing
      Set objWord = Nothing
      
    End Sub

    For more information, click here to refer about Table Object (Word)

    • Proposed as answer by David_JunFeng Sunday, June 26, 2016 2:56 PM
    • Marked as answer by David_JunFeng Monday, June 27, 2016 9:45 AM
    Thursday, June 16, 2016 5:56 AM

All replies

  • >>>owever, I am in need of VBA codes that will take the XML information and transform them into actual tables that adjust in size based on the number of columns/rows present in the XML. I'm not sure what to do to create a table and was hoping to get some tips to send me in the right direction of how to write this code.<<<

    According to your description, you could use MSXML to parse XML, you could refer to below links:

    MSXML

    A Beginner's Guide to the XML DOM

    and create table with VBA, you could refer to below code:
    Sub Demo()
    
      intNoOfRows = 5
    
      intNoOfColumns = 3
    
      Set objWord = CreateObject("Word.Application")
    
      objWord.Visible = True
    
      Set objDoc = objWord.Documents.Add
    
      Set objRange = objDoc.Range
    
      objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns
    
      Set objTable = objDoc.Tables(1)
    
      objTable.Borders.Enable = True
    
      For i = 1 To intNoOfRows
    
         For j = 1 To intNoOfColumns
    
         objTable.Cell(i, j).Range.Text = "Sumit_" & i & j
    
         Next
         
      Next
      
      Set objDoc = Nothing
      Set objWord = Nothing
      
    End Sub

    For more information, click here to refer about Table Object (Word)

    • Proposed as answer by David_JunFeng Sunday, June 26, 2016 2:56 PM
    • Marked as answer by David_JunFeng Monday, June 27, 2016 9:45 AM
    Thursday, June 16, 2016 5:56 AM
  • Thank you for responding, David, but that didn't really solve my issue.

    I'm already setup and using DOMs to extract XML information from the XML and pasting it into the word doc.
    The problem is I have a code that takes table information from the XML and places it in the word document as a linearized table with repeating headers and I'm not sure how to edit/change the code so that it displays an actual table. I know there is a major change that needs to happen, but I am so new to VBA, that I do not have the knowledge of what logic should replace other logic in the code to create a graphic table.

    Here's an example of the linearized style of table I'm currently using:

    Public Sub TblRegular(ByVal CCtag As String, ByVal xPath As String)
    
        Dim curConControl As ContentControls
        Set curConControl = ActiveDocument.SelectContentControlsByTag(CCtag)
        Dim tableInfo As Word.Table
        tablestring = ""
        Dim xDoc As MSXML2.DOMDocument
        Set xDoc = New MSXML2.DOMDocument
        
        If Not xDoc.Load(xml) Then
            Err.Raise xDoc.parseError.ErrorCode, , xDoc.parseError.reason
        End If
    
        Dim list As IXMLDOMNodeList
        Set list = xDoc.SelectNodes(xPath)
    
        Dim attr As IXMLDOMAttribute
        Dim node As IXMLDOMNode
        Dim childNode As IXMLDOMNode
    
        For Each node In list
    
            If node.HasChildNodes = True Then
                For Each childNode In node.ChildNodes
                    If childNode.HasChildNodes = True Then
                        For Each Child In childNode.ChildNodes
                            If Child.Text <> "" Then
                               If StrComp(Child.BaseName, "COLUMN_1", 1) = 0 Then
    '                                Debug.Print Child.Text
                                    tablestring = tablestring & Replace(Child.Text, "***********", "****")
                                Else
    '                                Debug.Print Child.Text
                                    tablestring = tablestring & ":" & Replace(Child.Text, "***********", "****")
                                End If
                            End If
                        Next Child
                    End If
    '                Debug.Print vbCrLf
                    tablestring = tablestring & vbCrLf
                Next childNode
            End If
        Next node
        
        With curConControl(1)
    '         Debug.Print Len(tablestring)
             If Len(tablestring) > 2 Then
                .Range.Text = tablestring
             Else
                ActiveDocument.Range(.Range.Start - 1, .Range.End + 2).Select
                Selection.Delete
             End If
        End With
    End Sub

    I have a feeling that the entire Node section is what needs to change because normal tables don't really read tables the same way to print them.

    I hope that all makes sense.

    Thursday, June 30, 2016 2:02 PM
  • >>>The problem is I have a code that takes table information from the XML and places it in the word document as a linearized table with repeating headers and I'm not sure how to edit/change the code so that it displays an actual table.

    According to your description, if you want to set Word Table Heading Row Repeat, you could refer to below:
    With ActiveDocument.Tables(1)
        .Rows(1).HeadingFormat = True
    End With
    If you want to set table style, you could refer to below code:
    ' create one new TableStyle
    Sub NewTableStyle()
     Dim styTable As Style
     
     Set styTable = ActiveDocument.Styles.Add( _
     Name:="TableStyle 1", Type:=wdStyleTypeTable)
     
     With styTable.Table
     
     'Apply borders around table
     .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
     .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
     .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
     .Borders(wdBorderRight).LineStyle = wdLineStyleSingle
     
     'Apply a double border to the heading row
     .Condition(wdFirstRow).Borders(wdBorderBottom) _
     .LineStyle = wdLineStyleDouble
     
     'Apply a double border to the last column
     .Condition(wdLastColumn).Borders(wdBorderLeft) _
     .LineStyle = wdLineStyleDouble
     
     'Apply shading to last row
     .Condition(wdLastRow).Shading _
     .BackgroundPatternColor = wdColorGray125
     
     End With
    End Sub
    then you could like below:
    With ActiveDocument.Tables(1)
        .Style = "TableStyle 1"
    End With
    For more information, click here to refer about TableStyle Object (Word)

    If I have any misunderstandings on your question, you could provide more information about your issue, for example full sample code, screenshot etc., that will help us reproduce and resolve it.

    Thanks for your understanding.
    Friday, July 1, 2016 6:35 AM