Adding Fields to Word Tables through VBA

H

Huw

Hello,
I am trying to write code to programatically create a table in a word footer
and insert fields into this table. When recording the macro that word creates
it show's up as using the selection object but I want to use the range object
(which should have all the functionality and more of the selection object).
However when running the code it comes up with error 4605 - "The command is
not available."
Here is the snippet of code -

Sub test()
Dim oFooter As HeaderFooter
Dim oSection As Section
Dim oRange As Range
Dim oTable As Table
Dim oCell As Cell
' Add the table and data to the footer
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
Set oRange = oFooter.Range
Set oTable = oRange.Tables.Add(oRange, 1, 3)
Set oCell = oTable.Cell(1, 1)
Set oRange = oCell.Range
oRange.Fields.Add oRange, wdFieldPage
Next oFooter
Next oSection
End Sub

Has anyone got a workaround or a reason why this happens. It happens on all
platforms of word (tried it on word 2002 SP3 and word 97 SR 2)

Thanks,

Huw
 
H

Helmut Weber

Hi.
your range includes the end of cell mark,
therefore
Set oRange = oCell.Range
oRange.End = oRange.End - 2 ' seems to work with -1 as well
oRange.Fields.Add oRange, wdFieldPage

But then you'll probably run into another problem
as even in a new, absolutely blank document, this
is true, here and now.
MsgBox ActiveDocument.Sections(1).Footers.Count ' 3

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Huw

Helmut,

Thanks for this. I can error trap for any inconsistencies. Should be able to
move forward with this now.

Huw
 

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