Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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)


SQL
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
Posted
Comments
PhilLenoir 21-Aug-14 9:51am    
Sorry, I don't have time to test a solution, but it looks to me that you are effectively trying to get Word to create a table in the first cell of an existing table. When I've populated a table in Word from a database, I've had an empty row, I put my insert point into the first cell of that row, write my first field, advance to the next cell, repeat for each field and add a row to the table for each subsequent record and repeat for each record. There may be a faster way, but I had to have provisional behaviour based on the data, so enumeration was the way to go.

If you don't already do this, I'd suggest recording a macro to mimic this behaviour and hand coding it to use your recordset.

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:

VB
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.
 
Share this answer
 
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,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900