Tables in Header using VBA

B

Bryan

Hello All :

Would appreciate sample code or help in placing TWO tables in the header of
a word document using VBA. I can put one table in the header, but trying two
gives me a error 6028. range can not be deleted.

Here is what I have so far for adding the table


Dim r As Range, t As Table, t2 as Table
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set t = ActiveDocument.Tables.Add(Range:=r2, NumRows:=1, NumColumns:=3)

With t
.Rows.Height = 30
.Columns(1).SetWidth 500, wdAdjustSameWidth
.Cell(1, 2).Range.Bold = True
.Columns(2).SetWidth 500, wdAdjustSameWidth
.Columns(3).SetWidth 200, wdAdjustSameWidth
End With

""'error when adding a second table 6082 Can not delete Range"""
Set t = ActiveDocument.Tables.Add(Range:=r2, NumRows:=1, NumColumns:=6)

With t2
........
end with
 
B

Bryan

Typo in my original post.
Set t = ActiveDocument.Tables.Add(Range:=r2, NumRows:=1, NumColumns:=6)
should be
Set t2 = ActiveDocument.Tables.Add(Range:=r2, NumRows:=1, NumColumns:=6)

but it still fails and returns a error 6028 message.
 
C

Cindy M -WordMVP-

Hi Bryan,

Even given the correction in your follow-up message, you don't show us r2. It's
not declared ("Dimmed") and it's not instantiated. Recommendation: put Option
Explicit at the top of your module.

Beyond that, please describe WHERE the second table should be located. In a
cell of the first? Below the first? (In that case, you need an empty paragraph
between the two).
Would appreciate sample code or help in placing TWO tables in the header of
a word document using VBA. I can put one table in the header, but trying two
gives me a error 6028. range can not be deleted.

Here is what I have so far for adding the table


Dim r As Range, t As Table, t2 as Table
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set t = ActiveDocument.Tables.Add(Range:=r2, NumRows:=1, NumColumns:=3)

With t
.Rows.Height = 30
.Columns(1).SetWidth 500, wdAdjustSameWidth
.Cell(1, 2).Range.Bold = True
.Columns(2).SetWidth 500, wdAdjustSameWidth
.Columns(3).SetWidth 200, wdAdjustSameWidth
End With

""'error when adding a second table 6082 Can not delete Range"""
Set t = ActiveDocument.Tables.Add(Range:=r2, NumRows:=1, NumColumns:=6)

With t2

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
B

Bryan

Thanks Cindy:

Wish to enter the second table below the first table in the header. The r2
is actually r (another typo) as the r2 statement was commented out in my
code and I cut and pasted the wrong line when I posted to the group. I will
try the empty paragraph between the two tables. thanks for the tip.

Cheers

-=Bryan=-
 
C

Cindy M -WordMVP-

Hi Bryan,
Wish to enter the second table below the first table in the header. The r2
is actually r (another typo) as the r2 statement was commented out in my
code and I cut and pasted the wrong line when I posted to the group. I will
try the empty paragraph between the two tables.
A couple of further thoughts:

1. If you don't want an empty paragraph (space) between the two tables, create
just the one table with the total number of rows. you should then be able to
merge or split cells in the "bottom half" so that it looks like two different
tables.

2. In order to "get below" the first table:

Assign a range to the entire first table: Set r2 = t1.Range
Collapse the range to it's endpoint, so that it's outside the table:
r2.Collapse wdCollapseEnd
Add the paragraph: r2.InsertAfter vbCR
Collapse again, then generate the new table

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
B

Bryan

Thank you very much Cindy. Do you have any quick sample code for merging
cells in rows that you could post.
 
C

Cindy M -WordMVP-

Hi Bryan,
Do you have any quick sample code for merging
cells in rows that you could post.
This would merge cels one through three in the second row

Sub MergeCellsInRow()
Dim tbl As Word.Table
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
Set cel = tbl.Rows(2).Cells(3)
tbl.Rows(2).Cells(1).Merge cel
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
B

Bryan

Thank you very much Cindy.

Cindy M -WordMVP- said:
Hi Bryan,

This would merge cels one through three in the second row

Sub MergeCellsInRow()
Dim tbl As Word.Table
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
Set cel = tbl.Rows(2).Cells(3)
tbl.Rows(2).Cells(1).Merge cel
End Sub


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)


This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
B

Bryan

Hi Cindy:

Do you have any sample code for creating a checkbox in a table cell using
automation/vba. Greatly appreciated. thanks

-=Bryan=-
 
C

Cindy M.

Hi Bryan,
Do you have any sample code for creating a checkbox in a table cell using
automation/vba.
1. Please start a new message, as this has absolutely no relationship to the
previous discussion

2. Please specify the Type of checkbox required (Word has many
possibilities). If you aren't certain, describe what functionality (if any)
the checkbox should have.

3. Please give the version of Word.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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