none
Populating Word Table with Data RRS feed

  • Question

  • Hello,

    Attempting to populate a word table via vb.net app (or programmatically) but having some difficulty. The data is being populated in the first column of the heading rather than the first row following the heading. Is there a way to determine where to start populating the table? Here's a sample of the code:

    Dim sData As String = ""
    
    Do While Not .EOF
    sData = sData & .Fields("transaction_date") & vbTab & .Fields("transaction_desc") & vbTab & .Fields("time_billed") & vbCr
    Loop
    
    moWordDoc.Tables.Item(iTableNum).Range.Text = sData
            moWordDoc.Tables.Item(iTableNum).Range.ConvertToTable(Separator:=WdTableFieldSeparator.wdSeparateByTabs, _
                                                                  AutoFitBehavior:=WdAutoFitBehavior.wdAutoFitFixed, _
                                                                  DefaultTableBehavior:=WdDefaultTableBehavior.wdWord8TableBehavior)

    The data is taken from an ADO.Net Dataset and stored into an tab-delimited string. The string is assigned to a Word table's Range to be converted into the table.

    Any help is appreciated

    
    

    jim


    • Edited by jim101 Wednesday, August 20, 2014 6:51 PM correct title of post
    Wednesday, August 20, 2014 6:50 PM

Answers

  • Hello,

    The problem has been solved by updating the template files from 2003 and 2007 to 2010. My apologies for the hassle. Thanks again for all of the suggestions and input. It is appreciated.

    Kind regards,


    jim

    • Marked as answer by jim101 Friday, August 22, 2014 3:07 AM
    Friday, August 22, 2014 3:06 AM

All replies

  • I take it that there are tables already present that you wish to add to? In that case you probably need something more like

    Dim oTbl As Table
    Dim oRow As Row

    Set oTbl = moWordDoc.Tables.Item(iTableNum)
        Do While Not .EOF
            Set oRow = oTbl.Rows.Add
            oRow.Cells(1).Range.Text = .Fields("transaction_date")
            oRow.Cells(2).Range.Text = .Fields("transaction_desc")
            oRow.Cells(3).Range.Text = .Fields("time_billed")
        Loop


    Graham Mayor - Word MVP
    www.gmayor.com

    Thursday, August 21, 2014 1:19 PM
  • Hi Graham,

    Thanks for the reply. It's appreciated. In the past, I was using the methodology you suggest, however, I ran into problems when the number of records in a DataSet exceeded the number of preset rows in the Word table, whereby rows had to added on the fly. This not only slowed the performance but also over-filled or over-ran other portions of the table. For example, the Word table has a section or rows for listing services or products and below those rows is the total amount for those products or services (like any other invoice). However, when additional rows have to be added, to list products, it overruns the total amount section. Here's a sample of the code:

    With moWordDoc.Tables.Item(iTableNum)
        Do While Not moAdoSql.EOF
            RowCnt = RowCnt + 1
                    If RowCnt > .Rows.Count - iNumOfRowsBeforeInsert Then
                        .Rows.Add(.Rows.Item(RowCnt - 1))
                    End If
    
                    .Cell(RowCnt - 1, COL_date).Range.Text = VB6.Format(NullToString(moAdoSql.Fields("transaction_date")), gcFORMAT_ShortDate)
                    .Cell(RowCnt - 1, COL_desc).Range.Text = NullToString(moAdoSql.Fields("transaction_desc"))
                    .Cell(RowCnt - 1, COL_time).Range.Text = NullToString(moAdoSql.Fields("time_billed"))
    moAdoSql.MoveNext()
                Loop
            End With
    

    As a result, per Microsoft article, http://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx, I'm trying to use the ConvertToTable method.

    Any help is appreciated.


    jim

    Thursday, August 21, 2014 5:16 PM
  • From your recordset determine the number of rows required then add them in one hit (should be faster). Then you can use Graham's code to copy the data.

    If you are working from vb.net I would get your basic Word code working first in Word VBA. Use the VBA timer function to measure how long each method takes. Once your code works in Word VBA its then easy to convert to VB.Net code.


    Rod Gill
    Author of the one and only Project VBA Book
    www.project-systems.co.nz

    Thursday, August 21, 2014 8:10 PM
  • Hello,

    The problem has been solved by updating the template files from 2003 and 2007 to 2010. My apologies for the hassle. Thanks again for all of the suggestions and input. It is appreciated.

    Kind regards,


    jim

    • Marked as answer by jim101 Friday, August 22, 2014 3:07 AM
    Friday, August 22, 2014 3:06 AM