Adding multiple tables - nesting problem

A

aklein

Hi.

I am trying to insert multiple tables in my Word document. I am
actually adding them in reverse order (ie: add one table, then add
another one BEFORE it ... and another one BEFORE that one). (The
reason I am doing this is somewhat hard to explain.... but will go
into it if someone needs to know!)

The problem is that each time I add a new table, it seems to nest
itself in the first cell of the table I had previously created.

I am trying to get to the point BEFORE the table so I can add a table
without it being nested.

The relevant code:
(The missing code opens Word - as this is called from Access - based
on a template that has the bookmark "GroupList" and sets the
ActiveDocument).
--------------------------
Dim objWord as Word.Application
Dim oRng as Word.Range

Set oRng = objWord.ActiveDocument.Bookmarks("Grouplist").Range
oRng.Select

'BUILD THE TABLE WITH BORDERS ETC.
'THIS CODE IS IN A LOOP THAT READS FROM A RECORDSET

While not rst.EOF
Set objTable =
objWord.ActiveDocument.Tables.Add(Range:=oRng, NumRows:=10,
NumColumns:=3)
objTable.Borders.OutsideLineStyle = wdLineStyleSingle
objTable.Borders.OutsideLineWidth = wdLineWidth025pt
objTable.Borders.InsideLineStyle = wdLineStyleSingle

objTable.PreferredWidthType = wdPreferredWidthPercent
objTable.PreferredWidth = 100

'USED FOR SOME PURPOSE LATER ON
tablecount = tablecount + 1


With objTable
myRowCount = 1

'TABLE IS POPULATED BY THE rst RECORDSET'S DATA USING
'.Cell(myRowCount,myColumn).Range.Text
'AS THIS PIECE IS WORKING... I SNIPPED IT OUT

'ONCE THE TABLE HAS ALL THE DATA... I DO THE FOLLOWING

'try to get before the table !
oRng.Collapse (wdCollapseStart)
oRng.InsertParagraph

oRng.Move wdCharacter, -1
oRng.InsertAfter ""

'get before that
oRng.Collapse (wdCollapseStart)
'add the bookmard back in to use again?
objWord.ActiveDocument.Bookmarks.Add ("GroupList")
oRng =
objWord.ActiveDocument.Bookmarks("GroupList").Range
oRng.Select
oRng.Collapse (wdCollapseEnd)
End With
'are done with this table
Set objTable = Nothing

'move on to the next record and add another table...
rst.movenext

wend

-----------------------------------------------
I am sure the secret has something to do with the Range... but I have
been unsuccessful with all my attempts. I am new to Word VBA (though
not Access).

Any help would be appreciated!
Aliza
 
G

Greg Maxey

Something like this should work:

Sub Test()
Dim i As Long
Dim myRowCount As Long
Dim oRng As Word.Range
Dim objTable As Word.Table
Set oRng = ActiveDocument.Bookmarks("Grouplist").Range
For i = 1 To 5
Set objTable = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=10,
NumColumns:=3)
With objTable
.Borders.OutsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineWidth = wdLineWidth025pt
.Borders.InsideLineStyle = wdLineStyleSingle
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.Range.InsertBreak Type:=wdColumnBreak
End With
myRowCount = 1
'try to get before the table !
oRng.Start = objTable.Range.Start - 1
oRng.Collapse (wdCollapseStart)
'add the bookmard back in to use again?
ActiveDocument.Bookmarks.Add "GroupList", oRng
oRng = ActiveDocument.Bookmarks("GroupList").Range
Set objTable = Nothing
Next i
End Sub
 
A

aklein

Thanks so much Greg!

It works like a charm!

The only issue I still have relates to some of the "details" I left
out in the original post (as I thought they weren't relevant).

The document I am working with is a template full of bookmarks.

Directly preceding the "GroupList" bookmark (where I insert the tables
as per your code)... is a "Summary" bookmark.

Prior to referencing the "GroupList" bookmark, I insert a file at the
"Summary" bookmark:

Set oRng = objWord.ActiveDocument.Bookmarks("summary").Range
oRng.Select
tmpFileName = Application.CurrentProject.Path & myFileName
objWord.Selection.Range.InsertFile FileName:=tmpFileName

And the "GroupList" bookmark is actually a bit more complicated than I
initially described (as I was trying to focus on the biggest issue at
hand).,,

There are actually 2 different things that could be done at the
"GroupList" bookmark:

1) Insert a table with data (working great, thanks to you!)
2) Insert a file

(Basically - the end result is a document that has an inserted file,
followed by associated tables... and then another inserted file...
followed by associated tables etc.)

The tables are great... the InsertFiles that appear between groups of
tables is great as well...

The issue is that the FIRST file that appears in the
"GroupList" (which is actually the LAST "GroupList" item to be
inserted since the Word document is being built from the bottom up)...
messes up the formatting of the file that was previously inserted (ie:
at the "Summary" bookmark) - removing the final carriage return of
that document.

So basically the code runs as follows:

.... Insert the file for the Summary bookmark (seems to be OK... the
code is pasted above)
.... Look at the data for the GroupList bookmark... if it is a table
you want... build it (as per your code).... if it is a file you want -
insert it!

The code I use for inserting the file at the GroupList bookmark is as
follows:

tmpFileName = Application.CurrentProject.Path &
myFileName2
oRng.InsertFile FileName:=tmpFileName

I do not Collapse the range... or set the start (since it appeared to
be working).

I think the code you gave me sends me back one space... which works
great to get out of the table - but seems to backspace one space too
far.

Any ideas?
Or better yet - any place I can read up on the proper use of Ranges
(ie: when to Collapse and what it all means?). Your code makes 100%
sense to me but it is unclear to me when Collapsing is necessary.

Thanks again (especially for the speed!)
Aliza
 
G

Greg Maxey

I might not be following your line of thought, but it seems to me that
you would need to relocate both "summary" and "GroupList" bookmarks
above the previous "summary" bookmark with each iteration. Something
like this:

Sub Test()
Dim i As Long
Dim myRowCount As Long
Dim oRng As Word.Range
Dim oSourceDoc As Word.Document
Dim bTable As Boolean
Dim objTable As Word.Table
For i = 1 To 5
Set oRng = ActiveDocument.Bookmarks("summary").Range
Set oSourceDoc = Documents.Open("C:\Test.doc", Visible:=False)
oRng.Text = oSourceDoc.Range.FormattedText
oRng.InsertBefore vbCr + vbCr
Set oRng = ActiveDocument.Bookmarks("Grouplist").Range
bTable = False 'For testing
If bTable Then
Set objTable = ActiveDocument.Tables.Add(Range:=oRng, NumRows:=10,
NumColumns:=3)
With objTable
.Borders.OutsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineWidth = wdLineWidth025pt
.Borders.InsideLineStyle = wdLineStyleSingle
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
'.Range.InsertBreak Type:=wdColumnBreak
End With
Set oRng = ActiveDocument.Bookmarks("summary").Range
oRng.Start = oRng.Start + 1
ActiveDocument.Bookmarks.Add "GroupList", oRng
Set objTable = Nothing
Else
Set oRng = ActiveDocument.Bookmarks("GroupList").Range
Set oSourceDoc = Documents.Open("C:\Test.doc", Visible:=False)
oRng.Text = oSourceDoc.Range.FormattedText
Set oRng = ActiveDocument.Bookmarks("summary").Range
oRng.Start = oRng.Start + 1
ActiveDocument.Bookmarks.Add "GroupList", oRng
End If
Next i
End Sub
 
G

Greg Maxey

I forgot to close the source file. Add before the Next i line:

oSourceDoc.Close wdDoNotSaveChanges
Or better yet - any place I can read up on the proper use of Ranges
(ie: when to Collapse and what it all means?). Your code makes 100%
sense to me but it is unclear to me when Collapsing is necessary.

Trial and error my friend. That is how I have learned the little I
know ;-)
 

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