One reader recently took up my challenge to not run screaming from macros … and was a little disappointed in the results.

I defined a macro successfully [to insert “Page” and the page number], with a button on the Quick Access Tool Bar. It works until I log off. After re-entering and trying the macro, I get an error message “Run Time error 5941 The requested member of the collection does not exist.” Any suggestions?

A macro that only runs until you log off. Well, that’s a puzzler.

As I told him in our email exchange, I’ve noticed that in Word 2010, macros are sometimes spotty in picking up settings from actions you take in the Ribbon. (It used to work fine — not sure what’s changed.) I myself had a similar problem with a macro I tried to do for a watermark.

I suspected he was attempting to embed the page number command using Word’s Insert Page Number command from the Header/Footer Tools contextual ribbon menu:

The problem there is the contextual part. I think the macro was freaking out because it didn’t find the Header/Footer Tools menu from which to draw the Insert Page Number command, so it scrambled. (The whole “macro disappears when I log out” thing? I got no clue.)

So I suggested he program the “Insert Page Number” thing into his macro from the Quick Parts menu instead:

video thumbnail

For you code jockeys out there, here’s how that works out:

Sub test()<br />
'<br />
' test Macro<br />
'<br />
'<br />
Selection.TypeText Text:="Page "<br />
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _<br />
"PAGE * Arabic ", PreserveFormatting:=True<br />
End Sub

Reader’s response?

Worked perfectly.  Thanks again for your help.

Just what I like to hear!

Similar Posts


  1. It’s actually two different field codes (signified below by <>):

    The literal text “Page ” + + the literal text ” of ” +

    1. May I beg your indulgence and ask you to incorporate that into the actual line of code? I’m afraid it doesn’t make sense to me in this context. Spot the newbie.

  2. I was able to get the Page X of Y to work as a Macro in Word 2007 with the following code:

    Selection.TypeText Text:=”Page ”
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    “PAGE \* Arabic “, PreserveFormatting:=True
    Selection.TypeText Text:=” of ”
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    “NUMPAGES \* Arabic “, PreserveFormatting:=True

  3. Thanks for pointing me in the right direction! I had the same problem as the first person (the macro would work until I exited Word but would give that same error message when I came back and tried it a second time). Just using the Quick Parts Field wasn’t the whole answer for me. I had it enter/create the header using the Edit Header command and then insert the page number with the Field. ANY double-clicking on the header area to open or close or insert anything resulted in an error message the second go-around.

  4. I’m wondering if you could expand this to centered at the bottom of the page and first page blank. I’ve been struggling with the placement of the number – tried doing it via footer – won’t preserve as quick-part.

    I want a macro that will insert a centered page number and keep the first page blank….thank you so much for any help.

  5. Hi Guru

    I am a complete and utter novice to recording macros and have the same problem as Shannon but cannot see your response to her. Can you help. I am trying to put a page number at the top centre of the second page (after a next page break) and no number on the first page (it needs to be an automatic number as further pages may follow. I have tried inserting the page number via page numbers, edit header, building blocks and quick parts but everytime I come out of the document it comes back with the runtime error 5941. Your instructions above are excellent and mainly in a language I can understand. Can you please help?

    I can provide you with the code I have used if you need it.

    Thank you.

    1. Having a different first-page header/footer is a function of Page Setup. To get to the Page Setup dialog, click the small arrow “launcher” at the bottom right corner of the Page Setup section on the Page Layout tab and select the Layout tab:

      Page Layout dialog

      See that checkbox that says “Different first page”? Bingo. Problem solved.

Comments are closed.