Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Tables / May 2008

Tip: Looking for answers? Try searching our database.

Problem inserting nested tables in VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Freaker - 09 May 2008 12:59 GMT
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
Jean-Guy Marcil - 09 May 2008 14:22 GMT
> Hello everyone!
>
[quoted text clipped - 16 lines]
>     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...
Freaker - 09 May 2008 21:09 GMT
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
Jean-Guy Marcil - 13 May 2008 14:00 GMT
> 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.

> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.