PDA

View Full Version : Solved: Multiple tables help



Mr Doubtfire
10-09-2005, 03:55 PM
I have tried a simple testing to create to two different tables with different column settings. BUT only to find the second one appears. Please help and code is as followed.: pray2:


Public objApp As Word.Application
Public Tbl1 As Table
Public Tbl2 As Table

Private Sub Command1_Click()

On Error Resume Next
Set objApp = GetObject(, "Word.Application")
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
End If
objApp.Documents.Open FileName:="c:\test\temp1.dot"
Set Tbl1 = objApp.ActiveDocument.Tables.Add _
(Range:=objApp.Selection.Range, _
NumRows:=2, NumColumns:=2)

Tbl1.Columns(1).Width = 100
Tbl1.Columns(2).Width = 100

Tbl1.Cell(1, 1).Range.Text = "Table1 - col1"
Tbl1.Cell(1, 2).Range.Text = "Table1 - col2"
Tbl1.Cell(2, 1).Range.Text = "VBA 1"
Tbl1.Cell(2, 2).Range.Text = "50%"

Set Tbl1 = Nothing
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 _
'<- move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove _
'<- move foward 1 line
Set Tbl2 = objApp.ActiveDocument.Tables.Add _
(Range:=objApp.Selection.Range, _
NumRows:=2, NumColumns:=2)

Tbl2.Columns(1).Width = 100
Tbl2.Columns(2).Width = 100

Tbl2.Cell(1, 1).Range.Text = "Table2 - col1"
Tbl2.Cell(1, 2).Range.Text = "Table2 - col2"
Tbl2.Cell(2, 1).Range.Text = "VBA 2"
Tbl2.Cell(2, 2).Range.Text = "100%"

Set Tbl2 = Nothing
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 _
'<- move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove _
'<- move foward 1 line

objApp.ActiveDocument.SaveAs FileName:="c:\test\test1.doc"

objApp.Quit False
Set objApp = Nothing

MsgBox "done"

End Sub

Bob Phillips
10-09-2005, 04:31 PM
Here's a shot at what I think you mean


Public objApp As Word.Application
Public Tbl1 As Table
Public Tbl2 As Table

Private Sub Command1_Click()

On Error Resume Next
Set objApp = GetObject(, "Word.Application")
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
End If
objApp.Documents.Open FileName:="c:\test\temp1.dot"

Set Tbl1 = objApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=2, _
NumColumns:=2)
Tbl1.Columns(1).Width = 100
Tbl1.Columns(2).Width = 100

Tbl1.Cell(1, 1).Range.Text = "Table1 - col1"
Tbl1.Cell(1, 2).Range.Text = "Table1 - col2"
Tbl1.Cell(2, 1).Range.Text = "VBA 1"
Tbl1.Cell(2, 2).Range.Text = "50%"

Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Set Tbl1 = Nothing

Set Tbl2 = objApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=2, _
NumColumns:=2)

Tbl2.Columns(1).Width = 100
Tbl2.Columns(2).Width = 100

Tbl2.Cell(1, 1).Range.Text = "Table2 - col1"
Tbl2.Cell(1, 2).Range.Text = "Table2 - col2"
Tbl2.Cell(2, 1).Range.Text = "VBA 2"
Tbl2.Cell(2, 2).Range.Text = "100%"

Set Tbl2 = Nothing
objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 _
'<- move to end of table
objApp.Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdMove _
'<- move foward 1 line

objApp.ActiveDocument.SaveAs FileName:="c:\test\test1.doc"

objApp.Quit False
Set objApp = Nothing

MsgBox "done"

End Sub

TonyJollans
10-09-2005, 04:33 PM
I'm not sure what you mean when you say only the second one appears but the root of your problem is that when you add a table immediately after another table, Word combines the two tables; try adding some space between them - before creating the second table do a Selection.TypeParagraph or something.

TonyJollans
10-09-2005, 04:35 PM
Beaten to it!

Incidentally, you shouldn't really Quit an application you've hooked in to using GetObject.

Bob Phillips
10-09-2005, 05:53 PM
Incidentally, you shouldn't really Quit an application you've hooked in to using GetObject.

Excellent point!

Mr Doubtfire
10-09-2005, 05:56 PM
Thank you to ALL!

Anne Troy
10-09-2005, 06:17 PM
Guys... I think it is incredibly cool to see someone like xld, who I believe is "Excel heavy" doing Word VBA. That's just ONE of the reasons I love this forum!!

Mr Doubt: You can mark your thread as solved using the Thread Tools dropdown at the top of the page. Of course, that is, if it IS solved. :)

Bob Phillips
10-10-2005, 01:46 AM
Guys... I think it is incredibly cool to see someone like xld, who I believe is "Excel heavy" doing Word VBA. That's just ONE of the reasons I love this forum!!

Thanks Dreamboat. My problem is I have rarely had a need to code for Word, but I do want to improve, so I figure that as I know the VBA pretty well, I have something to offer. By answering questions I will get better, and I will follow the threads actively and learn from others. A win-win I hope http://vbaexpress.com/forum/images/smilies/001.gif

MOS MASTER
10-13-2005, 02:15 PM
Guys... I think it is incredibly cool to see someone like xld, who I believe is "Excel heavy" doing Word VBA. That's just ONE of the reasons I love this forum!!

Agreed...nice to see you over here Bob! :yes

MOS MASTER
10-13-2005, 02:17 PM
By answering questions I will get better, and I will follow the threads actively and learn from others. A win-win I hope http://vbaexpress.com/forum/images/smilies/001.gif

Well that's the way I've been teaching it myself by helping others! :yes

Gaining knowledge and some new friends :cloud9: on the way!