Problem inserting nested tables in VBA

F

Freaker

Hello everyone!

I have had a problem for a few weeks now that noone in my Access group seems
to be able to answer. I am trying to layout a word document using tables, and
I am doing this from Access using Word automation.

I insert the first main table, and then I try to insert a nested table
within a cell of this main table. When I try to do this all I get is a nested
table with the right number of columns, but only one row. If I then run the
insert code a second time it formats the table correctly.

Does anyone have any idea why this would be? The code is below.


strBookmark = "Table"
objWord.ActiveDocument.Bookmarks(strBookmark).Select
Set objTable = objWord.ActiveDocument.Tables.Add(.Selection.Range,
intOps + 1, 3)

objTable.Cell(3, 3).Select
objTable.Tables.Add Selection.Range, 2, 2
 
J

Jean-Guy Marcil

Freaker said:
Hello everyone!

I have had a problem for a few weeks now that noone in my Access group seems
to be able to answer. I am trying to layout a word document using tables, and
I am doing this from Access using Word automation.

I insert the first main table, and then I try to insert a nested table
within a cell of this main table. When I try to do this all I get is a nested
table with the right number of columns, but only one row. If I then run the
insert code a second time it formats the table correctly.

Does anyone have any idea why this would be? The code is below.


strBookmark = "Table"
objWord.ActiveDocument.Bookmarks(strBookmark).Select
Set objTable = objWord.ActiveDocument.Tables.Add(.Selection.Range,
intOps + 1, 3)

objTable.Cell(3, 3).Select
objTable.Tables.Add Selection.Range, 2, 2

Normally, you should always use objects, declare them and set them. This is
even more important when automating Word from anohter app.

Also, never use the Selection object, unless there is no other way (e.g.
when inserting a shape in a header...). In your case, you do not need the
Selection object.

Finally, never use ActiveDocument when automating Word.

Here is sample code that does what your code does, but taking into
consideration the points I mention above (I have to twist things a bit since
I do ot have a document with a "Table" bookmark in it...):


Option Explicit

Sub test()

Dim docMain As Document
Dim rngTable As Range
Dim tblMain As Table
Dim tblNested As Table
Dim intOps As Long

Const strTbleBookMark As String = "Table"

intOps = 5

Set docMain = Application.Documents.Add
Set rngTable = docMain.Paragraphs(1).Range

With rngTable
.InsertParagraphAfter
.InsertParagraphAfter
.InsertParagraphAfter
.MoveEnd wdCharacter, -2
.Collapse wdCollapseEnd
.Bookmarks.Add strTbleBookMark, rngTable
End With

Set rngTable = docMain.Bookmarks(strTbleBookMark).Range
Set tblMain = docMain.Tables.Add(rngTable, intOps + 1, 3)
Set rngTable = tblMain.Cell(3, 3).Range
rngTable.Collapse wdCollapseStart
Set tblNested = docMain.Tables.Add(rngTable, 2, 2)

End Sub


With this code I can refer to both tables as I wish because I declared
separate objects for them.

I could have declared two different Range object, and would do so if I
needed to refer to each range again later in the code. Here, each range is
disposable once it has been used, so I just reset the same range object again
and again...
 
F

Freaker

Jean-Guy-Marcil
Thank you so much for what you have written. The terms I have seen you use
(particularly the .Collapse expression) do not come up in Access
documentation so I have not seen them before.

I will need to change it slightly to work within the Access framework but
what you have written is so clear that it will be easy to convert it, then I
can step through it which will help me emensly. I will try this tomorrow and
I will let you know what the results are

Thank you

Craig

:
 
J

Jean-Guy Marcil

Freaker said:
Jean-Guy-Marcil
Thank you so much for what you have written. The terms I have seen you use
(particularly the .Collapse expression) do not come up in Access
documentation so I have not seen them before.

Of course you haven't seen it in Access, it is part of the Word Object
Model. If you set a reference to the Word Object model from within the Acces
VBA editor, you should have acces to the Word VBA help files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top