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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert multiple tables into Word document using VB

Status
Not open for further replies.

nwb1

Programmer
Apr 28, 2004
39
GB
Hi friends,
Can any one let me know how to install multiple tables into a word document using VB please?

I can insert a table using following coding, but when I add the second table, it always insert a second table into the first table.

Dim wrdDoc As Word.Document
Dim wrdSelection As Word.Selection
Dim wrdApp As Word.Application
Set wrdApp = New Word.Application
wrdApp.Visible = True


Set wrdDoc = wrdApp.Documents.Add
wrdDoc.Select
Set wrdSelection = wrdApp.Selection
wrdSelection.TypeText "table1"
wrdApp.Selection.TypeParagraph

wrdDoc.Tables.Add wrdSelection.Range, NumRows:=1, NumColumns:=5
With wrdDoc.Tables(1)
.Cell(1, 1).Range.InsertAfter "col1"
.Cell(1, 2).Range.InsertAfter "col2"
.Cell(1, 3).Range.InsertAfter "col3"
.Cell(1, 4).Range.InsertAfter "col4"
.Cell(1, 5).Range.InsertAfter "col5"
End With

wrdSelection.TypeText "table2"
wrdApp.Selection.TypeParagraph
wrdDoc.Tables.Add wrdSelection.Range, NumRows:=1, NumColumns:=5

With wrdDoc.Tables(2)
.Cell(1, 1).Range.InsertAfter "col1"
.Cell(1, 2).Range.InsertAfter "col2"
End With

 
try:

Code:
Dim wrdDoc As Word.Document
Dim wrdSelection As Word.Selection
Dim wrdApp As Word.Application
Set wrdApp = New Word.Application
wrdApp.Visible = True
    
    
Set wrdDoc = wrdApp.Documents.Add
wrdDoc.Select
Set wrdSelection = wrdApp.Selection
wrdSelection.TypeText "table1"
wrdApp.Selection.TypeParagraph

wrdDoc.Tables.Add wrdSelection.Range, NumRows:=1, NumColumns:=5
     With wrdDoc.Tables(1)
          .Cell(1, 1).Range.InsertAfter "col1"
          .Cell(1, 2).Range.InsertAfter "col2"
          .Cell(1, 3).Range.InsertAfter "col3"
          .Cell(1, 4).Range.InsertAfter "col4"
          .Cell(1, 5).Range.InsertAfter "col5"
     End With

wrdSelection.MoveDown
wrdSelection.TypeParagraph

wrdSelection.TypeText "table2"
wrdApp.Selection.TypeParagraph
wrdDoc.Tables.Add wrdSelection.Range, NumRows:=1, NumColumns:=5

With wrdDoc.Tables(2)
          .Cell(1, 1).Range.InsertAfter "col1"
          .Cell(1, 2).Range.InsertAfter "col2"
End With
 
It works, though I have to count no of rows needs "movedown"

Many thnaks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top