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:
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
Worked perfectly. Thanks again for your help.
Just what I like to hear!