Newbie macro help please

B

Barnet

I'm trying to record a macro that will number the pages in my document. I
seem to do OK at first: name the macro, make a keyboard shortcut, begin
recording, execute the commands to put page numbers at the bottom center of
the document, and then stop recording.

But when I run the macro it stops with 'Runtime error 5941, the requested
member of the collection does not exist.' When I look at debug, it seems to
show all the steps except for adding the page number itself down in the
footer.

Also, since I've tried this a few times I seem to have to come up with new
names for the macro. Is there a way to erase all the old attempts and old
names to 'clear the slate'?

Thanks in advance,

Barnet
 
S

Suzanne S. Barnhill

Note that this should not require a macro. Just View | Header and Footer,
Switch Between Header and Footer, Ctrl+E to center (or tab once to the
centered tab stop), click on Insert Page Number, and all your pages will be
numbered. Save the document as a template if desired.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
B

Barnet

Thanks Terry and Suzanne,

I should say that I use MS Word 2007, so I don't have the Tools etc. pathway
to go. I found the dialogue that allowed me to erase previous attempts at
making macros, so that's good.

Suzanne, to be clear, I use the standard way of doing page numbers that
involves clicking on the Insert tab, then page numbering, then waiting while
the options load, then selecting bottom center, my consistent choice. I just
want to automate the process, so while your suggestion makes sense it doesn't
really save me the clicks. OTOH, the notion of creating a template makes
sense.

However, I'd still like to find out why I'm having trouble recording this
simple macro, since I'd probably make some other ones to automate certain
common tasks. That's the reason for macros, right? :)

So if someone still might help me figure out what's going wrong with my
current attempt I'd be very grateful.

Barnet
 
T

Terry Farrell

In Word 2007, the Macro button is on the Developer tab on the Ribbon. But
you need to enable the Developer Tab under Word Options, Popular Tab.

Terry
 
J

John... Visio MVP

Barnet said:
I'm trying to record a macro that will number the pages in my document. I
seem to do OK at first: name the macro, make a keyboard shortcut, begin
recording, execute the commands to put page numbers at the bottom center
of
the document, and then stop recording.

But when I run the macro it stops with 'Runtime error 5941, the requested
member of the collection does not exist.' When I look at debug, it seems
to
show all the steps except for adding the page number itself down in the
footer.

Also, since I've tried this a few times I seem to have to come up with new
names for the macro. Is there a way to erase all the old attempts and old
names to 'clear the slate'?

Thanks in advance,

Barnet


Your error was due to a line like
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Pg. Number
1").Insert Where:=Selection.Range, RichText:=True

I am not sure why, but it may be a matter of context.

You should be aware, that what the macro recorder records is not always
great code and there may be issues with replaying the macros. With the
Headers/Footers you need to switch windows between the main window and the
H/F window and that may be an issue.

John... Visio MVP
 
B

Barnet

Hi John,

Thanks so much, we're getting close. Let me show you what the debug window
displays:

WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries(" Blank").Insert _
Where:=Selection.Range, RichText:=True
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 2"). _
Insert Where:=Selection.Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Now, I know absolutely zippo about Visual Basic. I have noticed that it
seems to want to put "Plain Number 2" in, whereas I'd think it would want to
insert the symbol for successive page numbers (whatever that would be).

I have tried to create this macro both by going straight to the page number
tab, and also by first opening a footer, selecting "blank", and then using
the page number tab. I then have to use the Exit Footer (or whatever it's
called) button to get back to the main document.

But no matter what I try it ends up with the error messages above.

Thanks for taking the time with my question, I appreciate it.

Barnet
 
J

John... Visio MVP

I am not sure what is going on but
Debug.Print ActiveDocument.AttachedTemplate.BuildingBlockEntries.Count
results in a value of zero.


From another thread, it appears, according to Graham Mayor, the macro
recorder makes a mess of Autotext entries

:Word 2007 stores autotext entries differently from earlier versions and the
:macro recorder does not record entries that are stored in
:buildingblocks.dotx. In fact the macro recorder makes a complete hash of
:recording anything to do with autotext entries. You will have to modify the
:code by hand
:
:If your macro crashes on the line
:
:ActiveDocument.AttachedTemplate.BuildingBlockEntries("Plain Number 1"). _
:Insert Where:=Selection.Range, RichText:=True
:
then the entry is not stored in the document template where the line is
:looking for it.
:
:Either use the BuildingBlocks Organizer to move it to the document template
:blush:r
:to normal.dot and then use
:
:NormalTemplate.BuildingBlockEntries("Plain Number 1").Insert
:Where:=Selection.Range, _
:RichText:=True


Unfortunately, I have been unable to get the NormalTemplate to work. I still
end up with a count of zero.

John... Visio MVP
 
B

Barnet

Hi John,

To the extent I understand your post, it seems as if I'm stuck.

The somewhat odd thing is that the macro recorder seems to reach for
AutoText or BuildingBlocks, if that's similar. Is that where the page numbers
used by the Insert Page Number tab live?

Anyway, I guess that's the end of the road on this one. Seems very strange
that something seemingly so simple crashes.

Thanks again,

Barnet
 
J

John... Visio MVP

It is possible, but I think someone like Graham Mayor or Greg Maxek know the
workaround.

John..
 
D

Doug Robbins - Word MVP

Use:

Dim prange As Range
With ActiveDocument
Set prange = .Sections(1).Footers(wdHeaderFooterPrimary).Range
.Fields.Add Range:=prange, Type:=wdFieldEmpty, Text:="page"
prange.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

If you want the page number on the right, replace wdAlignParagraphCenter
with wdAlignParagraphRight, similarly for left.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Barnet

Are they people I can get in touch within some way? Or might they read this
here in the forum? Or can you ask them about this?

Barnet
 
J

John... Visio MVP

Barnet said:
Are they people I can get in touch within some way? Or might they read
this
here in the forum? Or can you ask them about this?

Barnet


They usually read the newsgroup, so I was hoping one of them (or any of the
other experts here) would see the bat signal and reply.
It seems Doug has risen to the challenge with an answer.

And before I get in trouble, it is Greg Maxey not Greg Maxek. Sorry about
that Greg.

John.. Visio MVP
 
D

Doug Robbins - Word MVP

I am not sure what you mean by

????????


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg Maxey

They usually read the newsgroup, so I was hoping one of them  (or any of the
other experts here) would see the bat signal and reply.
It seems Doug has risen to the challenge with an answer.

And before I get in trouble, it is Greg Maxey not Greg Maxek. Sorry about
that Greg.

John.. Visio MVP

John,

If I was upset my spelling errors then I would always be upset with
myself. No problem.

I haven't followed all of this thread, so what I am posting may have
already been addressed of of no value.

While it can often be a good place to start, the recorder can also
make a dog's breakfast out of any attempt to create macro. This is a
case in point. The problem is that the builidngblocks in question are
not part of the attached template but a one of the predefined building
blocks that ships with Word and stored in the Bluilding Blocks.dotx
file. Worse yet, this collection isn't available if Building
Blocks.dotx isn't loaded.

Writing from scratch you could use the following, but I believe that
Doug's suggestion is better.

Sub ScratchMacro()
Dim oTmp As Template
Templates.LoadBuildingBlocks
For Each oTmp In Templates
If oTmp.Name = "Building Blocks.dotx" Then Exit For
Next oTmp
oTmp.BuildingBlockEntries(" Blank").Insert Selection.Range
'Or
'oTmp.BuildingBlockTypes(wdTypeFooters).Categories("Built-
In").BuildingBlocks(" Blank").Insert Selection.Range
oTmp.BuildingBlockEntries("Plain Number 2").Insert Selection.Range
End Sub
 
J

John... Visio MVP

I think I threw him off be servely trimming your post so I could say thank
you.

John... Visio MVP
 
D

Doug Robbins - Word MVP

Yes, that is probably what it was. I wonder if he found my original post?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Barnet

Hi Doug,

Thanks for your time. From what I can tell, the other posters believe you've
come up with an answer to the question.

However, this being a newbie forum, and I myself being a newbie...

....well, I am utterly lost by the answer you've provided! :)

I tried Developer Tab --> Macros --> Edit and then pasted the code in the
box that came up. But nothing seemed to happen when I tried the shortcut keys.

Would you be kind enough to help me understand your answer, and how to
execute it?

Thanks very much,

Barnet
 

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

Similar Threads


Top