Word 2007 Macro Error when Changing Footer

D

DNatt2

Hello all,

I started the macro recorder in Word 2007 B2TR, and then did the following
steps:

1. Clicked the "Insert" tab on the ribbon.
2. Clicked Page Number > Bottom of Page > Bold Numbers 2. (This is one of
the selections under the "Page X of Y" category).
3. Stopped the Macro recorder.

Unfortunately, the very simple macro code that was generated (see below)
does not appear to work. If I immediately create a new document and try to
run the macro (By clicking "View > Macros > [select my macro in the list] >
Run", I get a dialog that says, "Run-time error '5941': The requested member
of the collection does not exist.". Here is the macro code:

Sub Macro1()

Macro1 Macro

WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 2"). _
Insert Where:=Selection.Range, RickText:=True
End Sub

When I click "Debug", it highlights the "ActiveDocument..." and "Insert..."
lines above. Needless to say, I get an error when calling this code in an
application outside Word as well. Can anybody help me figure out why this
isn't working?

The reason I need to fix this is that I already have macro code that I used
with Word 2000, XP, and 2003 that used the "AutoTextEntries" collection. I
was easily able to set an AutoTextEntry of "Page X of Y" to set up a footer
of that format. But AutoText seems to be nonfunctional in Word 2007, and I
can't figure out how to get a footer that says "Page X of Y" as a result.

Thanks, and please let me know if you need any more information to help me
diagnose my problem.

-D
 
C

Cindy M.

Hi =?Utf-8?B?RE5hdHQy?=,

The problem is that the macro recorder apparently hasn't been adjusted to
recognize when a building block isn't in Normal.dot or the AttachedTemplate. The
default set of Building Blocks are stored in a template named "Building
Blocks.dotx". The following macro code (typed, not copied, so watch out for
typos!) works for me

Sub InsertPageNums_CM()
dim templ as Word.Template
Dim templBB as Word.Template
WordBasic.ViewFooterOnly
For Each templ in Application.Templates
If Instr(temp.FullName, "Building Blocks.dotx") _
<> 0 Then
Set templBB = templ
Exit For
End If
Next
templ.BuildingBlockEntries("Bold Numbers 2"). _
Insert Where:=Selection.Range, RichText:=True
End Sub

BTW, excellent problem description. Made reproducing quite simple :)
I started the macro recorder in Word 2007 B2TR, and then did the following
steps:

1. Clicked the "Insert" tab on the ribbon.
2. Clicked Page Number > Bottom of Page > Bold Numbers 2. (This is one of
the selections under the "Page X of Y" category).
3. Stopped the Macro recorder.

Unfortunately, the very simple macro code that was generated (see below)
does not appear to work. If I immediately create a new document and try to
run the macro (By clicking "View > Macros > [select my macro in the list] >
Run", I get a dialog that says, "Run-time error '5941': The requested member
of the collection does not exist.". Here is the macro code:

Sub Macro1()

Macro1 Macro

WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 2"). _
Insert Where:=Selection.Range, RickText:=True
End Sub

When I click "Debug", it highlights the "ActiveDocument..." and "Insert..."
lines above. Needless to say, I get an error when calling this code in an
application outside Word as well. Can anybody help me figure out why this
isn't working?

The reason I need to fix this is that I already have macro code that I used
with Word 2000, XP, and 2003 that used the "AutoTextEntries" collection. I
was easily able to set an AutoTextEntry of "Page X of Y" to set up a footer
of that format. But AutoText seems to be nonfunctional in Word 2007, and I
can't figure out how to get a footer that says "Page X of Y" as a result.

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 :)
 
D

DNatt2

Hi Cindy,

Thanks for the reply. I tried the macro code you sent. First of all, I
think the "temp" in the If statement should be "templ". Assuming that's
right, whenever I run the macro code, I get "Run-time error '91': Object
variable or With block variable not set". When I click Debug, it highlights
the last two lines of the macro code (not including the "End Sub", of
course). Any further suggestions?

Also, I noticed that if I simply want Page Numbers in Word (without the
"Page X of Y" format), I can simply use the PageSetup > Sections > Section >
Headers > HeadersFooters > HeaderFooter > PageNumbers > Add method. Is there
any way to configure anything along that object path to get "Page X of Y"?
Furthermore, in Excel, it's *very* easy to do this...I can just get
ActiveSheet > PageSetup > RightFooter and set it to "Page %P of %N". Is
there any way to be able to set up the footer in Word this way without having
to deal with all that BuildingBlock nonsense?

Thanks, and I hope to hear from you soon...this is a major showstopper issue
for me.

-D

P.S. - Thanks for the compliment...I worked in tech support a while back, so
I know how annoying it is to get a bad problem description...

Cindy M. said:
Hi =?Utf-8?B?RE5hdHQy?=,

The problem is that the macro recorder apparently hasn't been adjusted to
recognize when a building block isn't in Normal.dot or the AttachedTemplate. The
default set of Building Blocks are stored in a template named "Building
Blocks.dotx". The following macro code (typed, not copied, so watch out for
typos!) works for me

Sub InsertPageNums_CM()
dim templ as Word.Template
Dim templBB as Word.Template
WordBasic.ViewFooterOnly
For Each templ in Application.Templates
If Instr(temp.FullName, "Building Blocks.dotx") _
<> 0 Then
Set templBB = templ
Exit For
End If
Next
templ.BuildingBlockEntries("Bold Numbers 2"). _
Insert Where:=Selection.Range, RichText:=True
End Sub

BTW, excellent problem description. Made reproducing quite simple :)
I started the macro recorder in Word 2007 B2TR, and then did the following
steps:

1. Clicked the "Insert" tab on the ribbon.
2. Clicked Page Number > Bottom of Page > Bold Numbers 2. (This is one of
the selections under the "Page X of Y" category).
3. Stopped the Macro recorder.

Unfortunately, the very simple macro code that was generated (see below)
does not appear to work. If I immediately create a new document and try to
run the macro (By clicking "View > Macros > [select my macro in the list] >
Run", I get a dialog that says, "Run-time error '5941': The requested member
of the collection does not exist.". Here is the macro code:

Sub Macro1()

Macro1 Macro

WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers 2"). _
Insert Where:=Selection.Range, RickText:=True
End Sub

When I click "Debug", it highlights the "ActiveDocument..." and "Insert..."
lines above. Needless to say, I get an error when calling this code in an
application outside Word as well. Can anybody help me figure out why this
isn't working?

The reason I need to fix this is that I already have macro code that I used
with Word 2000, XP, and 2003 that used the "AutoTextEntries" collection. I
was easily able to set an AutoTextEntry of "Page X of Y" to set up a footer
of that format. But AutoText seems to be nonfunctional in Word 2007, and I
can't figure out how to get a footer that says "Page X of Y" as a result.

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 :)
 
C

Cindy M.

Hi =?Utf-8?B?RE5hdHQy?=,
First of all, I
think the "temp" in the If statement should be "templ".
Yes, you're right. As I said, I had to type it all out...
Assuming that's
right, whenever I run the macro code, I get "Run-time error '91': Object
variable or With block variable not set". When I click Debug, it highlights
the last two lines of the macro code (not including the "End Sub", of
course).
Hmmmm <mutter-mutter> I've fixed the "temp" in the code below and corrected
something else. I don't think it's what the error message was about, but it was
definitely a logical error.

Aha! Running it today, I get the same error. I happen to know that Building
Blocks.dotx is the old, Beta 2 version on my machine and Beta2TR can't load it.
I always have to let Word "recover it" (I really need to delete it, once, but
leaving it lets me duplicate many problems people are having). Once I do that,
by manually selecting any entry, the macro will work.

I've changed it around a bit, to check for the template being present:

Sub InsertPageNums_CM()
dim templ as Word.Template
Dim templBB as Word.Template
For Each templ in Application.Templates
If Instr(templ.FullName, "Building Blocks.dotx") _
<> 0 Then
Set templBB = templ
Exit For
End If
Next
If Not (templBB Is Nothing) Then
WordBasic.ViewFooterOnly
templBB.BuildingBlockEntries("Bold Numbers 2"). _
Insert Where:=Selection.Range, RichText:=True
Else
MsgBox "Can't find Building Blocks.dotx"
End If
End Sub
Also, I noticed that if I simply want Page Numbers in Word (without the
"Page X of Y" format), I can simply use the PageSetup > Sections > Section >
Headers > HeadersFooters > HeaderFooter > PageNumbers > Add method. Is there
any way to configure anything along that object path to get "Page X of Y"?
Furthermore, in Excel, it's *very* easy to do this...I can just get
ActiveSheet > PageSetup > RightFooter and set it to "Page %P of %N". Is
there any way to be able to set up the footer in Word this way without having
to deal with all that BuildingBlock nonsense?
Excel's possibilities for headers and footers are very limited, compared to
Word. So, no, there's no direct equivalent for setting them up. But you can
certainly make life easier. Here are some suggestions

1. Put the "Quick Parts" or "Building Blocks Organizer" in the QAT, so that you
don't always have to go to the Insert tab. Just right-click on the tool you
prefer, then "Add to Quick Access Toolbar".

2. Create your own set of Building blocks, so that you can create an entire
header or footer by inserting just one building block, rather than numerous
separate steps (if you need more than just page numbers)

3. You can use a macro to create different kinds of headers/footers, with page
numbers, without accessing Building Blocks. Record these steps, to get an idea
of what I have in mind

- Start the macro recorder
- Insert/Footer/Edit Footer
- Press Ctrl+F9 (this inserts a pair of field brackets)
- Type: PAGE
- Press F9 (this should update the field and hide the field code)
- Press: HOME
- Type: Page
- Insert/Close Header and Footer
- Stop the macro recorder

Note that this isn't the way a professional programmer would do it. But it's the
simplest approach for someone who doesn't want to get into the dirty details of
handling the Word object model.

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 :)
 
D

DNatt2

Hi Cindy,

Thanks for the response. The macro suggestion you gave at the end is
actually the best way for me to solve this problem, since the environment in
which I'm calling this code really doesn't approve of the
BuildingBlockEntries stuff. :) Incidentally, I think I may also end up
creating the footer with the Page and NumPages fields to simply be "1/4",
representing "Page 1 of 4", since the app I'm working on will be in multiple
languages, and I really don't feel like figure out how the words "Page" and
"of" translate in at least 6 languages.

I appreciate the help. By the way, I don't have a way to test this in
non-English Office 2007...do you know if the "PAGE" and "NumPages" field
names will work in German Word 2007, French Word 2007, Japanese Word 2007,
etc.? I would imagine they'd have to, as macro code would be very brittle if
these field names changed between languages.

-D
 
C

Cindy M.

Hi =?Utf-8?B?RE5hdHQy?=,
I appreciate the help. By the way, I don't have a way to test this in
non-English Office 2007...do you know if the "PAGE" and "NumPages" field
names will work in German Word 2007, French Word 2007, Japanese Word 2007,
etc.?
They should work the same, although I can't be absolutely sure about the
Japanese :) but since Word 2000, all fields in German and French definitely
use the English field names. Supposedly, this is the case for all languages,
but these are the only other languages I've actually tested this on :)

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 :)
 
D

DNatt2

Thanks Cindy. It turns out I don't need to worry about the language issue,
because apparently one of the optional inputs on the "Add" method of the
Field class is "Type". I can simply specify a type of 'wdFieldPage' or
'wdFieldNumPages' instead of actually "typing" the field text with my macro.

I really appreciate your help in this matter...it had been confusing me for
a while.

-D

:
 

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