accessing table inside a positioning frame in header

G

Gina Meyer

Hi all.

I have a single table in my header. I'd like to use vba to set bookmarks
there.
add a bookmark "bm_Name" in first cell

I am trying to access this table but tables(1) doen't work.

The table is inside a positioning frame.

Searching through the net didn't give me an answer.

Maybe someone has the right idea as to how this ican be achieved ?!

many thanks
Gina :)
 
J

Jay Freedman

Hi all.

I have a single table in my header. I'd like to use vba to set bookmarks
there.
add a bookmark "bm_Name" in first cell

I am trying to access this table but tables(1) doen't work.

The table is inside a positioning frame.

Searching through the net didn't give me an answer.

Maybe someone has the right idea as to how this ican be achieved ?!

many thanks
Gina :)

The long way (for learning purposes, so you can see how the pieces fit
together) is

Sub x()
Dim hdr As HeaderFooter
Dim tbl As Table
Dim cel As Cell
Dim rg As Range

Set hdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set tbl = hdr.Range.Tables(1)
Set cel = tbl.Cell(1, 1)
Set rg = cel.Range
rg.Collapse direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add Name:="bm_Name", Range:=rg
End Sub


All that can be collapsed by stringing together the pieces with dots:


Sub y()
Dim rg As Range

Set rg = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Tables(1).Cell(1, 1).Range
rg.Collapse direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add Name:="bm_Name", Range:=rg
End Sub
 
G

Gina

Jay Freedman said:
The long way (for learning purposes, so you can see how the pieces fit
together) is

Sub x()
Dim hdr As HeaderFooter
Dim tbl As Table
Dim cel As Cell
Dim rg As Range

Set hdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set tbl = hdr.Range.Tables(1)
Set cel = tbl.Cell(1, 1)
Set rg = cel.Range
rg.Collapse direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add Name:="bm_Name", Range:=rg
End Sub


All that can be collapsed by stringing together the pieces with dots:


Sub y()
Dim rg As Range

Set rg = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Tables(1).Cell(1, 1).Range
rg.Collapse direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add Name:="bm_Name", Range:=rg
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.

Jay, thanks so much!!!

But still when stepping through the code and it comes down to Tables(1) I
get the error that the element isn't part of the collection

the table is inside a positioning frame

so I tried:

Dim hdr As HeaderFooter
Dim tbl As Table
Dim cel As Cell
Dim rg As Range
Dim frm As Frame

Set hdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set frm = hdr.Range.Frames(1)
Set tbl = frm.Range.Tables(1)
Set cel = tbl.Cell(1, 1)
Set rg = cel.Range
rg.Collapse Direction:=wdCollapseStart
....

on the line with Tables(1) I get the error that the element isn't part of
the collection

(this is obviously driving me crazy!) ... don't have a clue as to what is
wrong here

thx Gina
 
G

Gina

Jay Freedman said:
The long way (for learning purposes, so you can see how the pieces fit
together) is

Sub x()
Dim hdr As HeaderFooter
Dim tbl As Table
Dim cel As Cell
Dim rg As Range

Set hdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set tbl = hdr.Range.Tables(1)
Set cel = tbl.Cell(1, 1)
Set rg = cel.Range
rg.Collapse direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add Name:="bm_Name", Range:=rg
End Sub


All that can be collapsed by stringing together the pieces with dots:


Sub y()
Dim rg As Range

Set rg = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Tables(1).Cell(1, 1).Range
rg.Collapse direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add Name:="bm_Name", Range:=rg
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.

Jay thanks so much .... something is going on at least :)

the bookmarks are now set but bookmarks all at the same place at the
beginning of the inner table (tbl2)
despite I am talking to the cell - tbl2.Cell(2, 1) - it doesn't seem to
'move' downwards when setting the bookmarks!

here's my code again: ( hopefully showing what I am trying to achieve )
....
Set hdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set tbl = hdr.Range.Tables(1)
Set tbl2 = tbl.Range.Tables(1)

Set cel = tbl2.Cell(1, 1)
Set rg = cel.Range
rg.Collapse Direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add name:="bm_Anrede", Range:=rg

Set cel = tbl2.Cell(2, 1)
Set rg = cel.Range

ActiveDocument.Bookmarks.Add name:="bm_Nachname", Range:=rg

Set cel = tbl2.Cell(3, 1)
Set rg = cel.Range

ActiveDocument.Bookmarks.Add name:="bm_Adresszusatz", Range:=rg
 
J

Jay Freedman

Jay thanks so much .... something is going on at least :)

the bookmarks are now set but bookmarks all at the same place at the
beginning of the inner table (tbl2)
despite I am talking to the cell - tbl2.Cell(2, 1) - it doesn't seem to
'move' downwards when setting the bookmarks!

here's my code again: ( hopefully showing what I am trying to achieve )
...
Set hdr = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
Set tbl = hdr.Range.Tables(1)
Set tbl2 = tbl.Range.Tables(1)

Set cel = tbl2.Cell(1, 1)
Set rg = cel.Range
rg.Collapse Direction:=wdCollapseStart

ActiveDocument.Bookmarks.Add name:="bm_Anrede", Range:=rg

Set cel = tbl2.Cell(2, 1)
Set rg = cel.Range

ActiveDocument.Bookmarks.Add name:="bm_Nachname", Range:=rg

Set cel = tbl2.Cell(3, 1)
Set rg = cel.Range

ActiveDocument.Bookmarks.Add name:="bm_Adresszusatz", Range:=rg

Did you change the document to replace the table-in-a-frame with a table
nested inside a cell of another table? Or was the original "frame" really a
table all the time? I'm confused! :)

Anyway, assuming that you now have a three-row table nested inside Cell(1,1)
of the outer table, the trouble you're having is that the assignment

Set tbl2 = tbl.Range.Tables(1)

makes tbl2 point to the same outer table as tbl. The assignment you need to
get the inner table is

Set tbl2 = tbl.Cell(1, 1).Tables(1)

To make sure you're addressing the nested table, you can put this expression
in the Watch window of the VBA editor:

tbl2.NestingLevel

and look at its value as you single-step (with F8) through the assignment. The
outer table (tbl) has a NestingLevel of 1, and the inner table (tbl2) should
show a NestingLevel of 2.

One other thing to avoid a later problem: each time you assign rg, before you
insert its bookmark, you need to collapse the range again with

rg.Collapse Direction:=wdCollapseStart

Otherwise the bookmark includes the invisible cell marker, and inserting text
there can damage the table layout.
 
G

Gina

Jay Freedman said:
Did you change the document to replace the table-in-a-frame with a table
nested inside a cell of another table? Or was the original "frame" really
a
table all the time? I'm confused! :)

Anyway, assuming that you now have a three-row table nested inside
Cell(1,1)
of the outer table, the trouble you're having is that the assignment

Set tbl2 = tbl.Range.Tables(1)

makes tbl2 point to the same outer table as tbl. The assignment you need
to
get the inner table is

Set tbl2 = tbl.Cell(1, 1).Tables(1)

To make sure you're addressing the nested table, you can put this
expression
in the Watch window of the VBA editor:

tbl2.NestingLevel

and look at its value as you single-step (with F8) through the assignment.
The
outer table (tbl) has a NestingLevel of 1, and the inner table (tbl2)
should
show a NestingLevel of 2.

One other thing to avoid a later problem: each time you assign rg, before
you
insert its bookmark, you need to collapse the range again with

rg.Collapse Direction:=wdCollapseStart

Otherwise the bookmark includes the invisible cell marker, and inserting
text
there can damage the table layout.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.

Jay ... that is great ...

sorry, yes I changed the frame to a table (an inner nested)!!
reason: as I couldn't get anything to work with the frame so I decided to go
for a table for the time being

many thx for all your additional tips
it helped me a lot :)
happy easter!!!!

Gina :)
 

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