Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Freaker Freaker is offline
external usenet poster
 
Posts: 2
Default Problem inserting nested tables in VBA

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).Sele ct
Set objTable = objWord.ActiveDocument.Tables.Add(.Selection.Range ,
intOps + 1, 3)

objTable.Cell(3, 3).Select
objTable.Tables.Add Selection.Range, 2, 2
  #2   Report Post  
Posted to microsoft.public.word.tables
Jean-Guy Marcil[_2_] Jean-Guy Marcil[_2_] is offline
external usenet poster
 
Posts: 373
Default Problem inserting nested tables in VBA

"Freaker" wrote:

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).Sele ct
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...

  #3   Report Post  
Posted to microsoft.public.word.tables
Freaker Freaker is offline
external usenet poster
 
Posts: 2
Default Problem inserting nested tables in VBA

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" wrote:



  #4   Report Post  
Posted to microsoft.public.word.tables
Jean-Guy Marcil[_2_] Jean-Guy Marcil[_2_] is offline
external usenet poster
 
Posts: 373
Default Problem inserting nested tables in VBA

"Freaker" wrote:

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


Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Tables and Corruption Rene Tables 1 October 17th 07 06:12 PM
Weird nested tables problem audreybmorin Tables 2 August 9th 07 02:00 PM
Nested Tables - printing problem on multiple pages Victor_alb Tables 0 April 3rd 07 06:36 AM
For the archive, code to format (styles and column widths) tables and nested tables kbutterly Tables 0 January 24th 07 02:00 PM
Help with nested tables Anasazi Tables 2 March 18th 05 04:42 AM


All times are GMT +1. The time now is 05:43 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"