Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Row in MSWord table 1

Status
Not open for further replies.

Kreiss

Programmer
Oct 8, 2003
48
US
I'm populating a MS Word table through my VB code. I'm running a "For" loop to attempt to populate the a new row for each record which is where I'm having trouble figuring out how to add a new row to the table.

Thanks in advance,

Dim WordObject As Word.Application
Set WordObject = CreateObject("Word.Application")
Printing.Show vbModeless

With WordObject
.Documents.Open ("M:\VB_Projects\AH-OOS\Reports\ViolationSheet")
.Visible = True
For i = 0 To lstViolations.ListCount - 1

.ActiveDocument.Bookmarks("txtdispostion").Select
.Selection.Text = Left(lstViolations.List(i), 2)
.ActiveDocument.Tables("Table 1").Select
.Selection.InsertRows
Next i
Set WordObject = Nothing
End With
 
I usually do it the other way, by concatenating a string with all the table values with separators in one string in VB, the using the Word ConvertToTable Method. There is a good example in Word VBA help (Help|Index|ConvertToTable Method)

If you're still stuck, post back and I'll dig out a sample.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
If you have an example that would be helpful. I'm trying to figure how to concat a string from all of my textboxes.
 
This picks various SubItems from a Listview, concatenates them with a '$' separator character, with vbCrLf as a row separator, then converts the whole string to a table:

Code:
With lvwParts
For temp1 = 1 To count1  [COLOR=green]'listview rows[/color]
mystr = mystr & .ListItems(temp1).SubItems(3) & "$" & .ListItems(temp1).SubItems(1) & "$" & .ListItems(temp1).SubItems(2) & "$" & vbCrLf
Next temp1
End With
Set myrange = mydoc.Bookmarks("list").Range
myrange.InsertAfter mystr
With myrange
    .ConvertToTable Separator:="$", _
        NumColumns:=5, Format:=wdTableFormatGrid7, ApplyBorders:=True _
        , ApplyShading:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows _
        :=False, ApplyLastRow:=False, ApplyFirstColumn:=False, ApplyLastColumn:= _
        False, AutoFit:=True
End With

The easy way to get the ConvertToTable code is to record a macro in word and modify the recorded code to suit.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top