Run-time error 5941 - Not always, But Annoyingly Often

I have been recording Macros in MS Word since the mid-90's. Never mind. That experience appears to be irrelevant now.

Yesterday I recorded a whole bunch of very helpful Macros for the keyboard. Worked perfectly for many, many documents --

and then suddenly I get this cryptic message: Run-time error '5941' the requested member of the collection does not exist.

Now it wouldn't do much good to post the macro's vb code, because
None of the VB code has been altered in any way.   Nevertheless, all macros have suddenly and inexplicably been disabled. Sometime these macros perform flawlessly at times and then suddenly I get Run-time error '5941' the requested member of the collection does not exist.


Please note: I use exclusively Keyboard Commands: Cntrl/Alt/Letter.

Repeat: These macros run perfectly sometimes and then I get that Run-time error '5941' message.

I am no longer amused!


("Sidebar question": Why are some things that were so simple in much older versions of Word now so much more difficult??)



Answer
Answer
Bingo!

In order to minimize Word's startup time, Microsoft programmed it to not load building blocks into memory until the first time you do something that uses them -- such as clicking the Quick Parts button or using one of the galleries that insert a building block (page numbers, cover pages, canned headers or footers,...).

If you happen to do one of those actions before you run your macro, the template Built-In Building Blocks.dotx has been loaded and added to the Application.Templates collection. But if you haven't yet done any of those actions, the template hasn't been loaded... there is no member of the Templates collection corresponding to that name, and BOOM -- error 5941.

This is one of those things the macro recorder has no way to anticipate, so the code doesn't do anything to avoid it. You can add one line to fix it. After the comment lines at the beginning

'
' PageNumber Macro
'

add this line:

   Application.Templates.LoadBuildingBlocks

If the building blocks haven't been loaded yet, that will force them to load. If they have already been loaded, then that line doesn't do anything.
_____________________________
https://jay-freedman.info

49 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer
If you've been recording Word macros that long, you must have run into macro recordings that don't work properly, or at all, when played back -- either in certain situations or every time they run. I've written a whole page about such problems (http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm) and barely scratched the surface.

The message about "the requested member of the collection" is a very common one. Your code tries to do something with one of Word's many "collections" such as Paragraphs, Fields, Tables, and so on -- and in a particular document that specific collection may have no members (for example, the macro tries to add a row to a table, but the document doesn't contain any tables). Or the macro tries to do something with a specific numbered member, like ActiveDocument.Tables(3), but this document contains only a smaller number of those things, like there's only one table.

You can spot a collection in VBA code because its name is plural, as in the examples I mentioned.

When the error occurs, you get the choice to exit the macro or to debug it. Click the Debug button, and look at the line of code in the macro that has a yellow highlight. What does that line say?

While the macro is paused in debug mode, you can also select the collection in that line (for example, ActiveDocument.Tables), right-click it, and choose Add Watch; then click OK in the little dialog. You'll get a window at the bottom of the editor, with the collection's name; click the plus-in-a-box to its left to expand it. You can see its Count property, which says how many items are in the collection; and if the count is greater than zero you can see Item 1, Item 2, etc. that can be expanded to show the properties of the individual members.

To your "sidebar question": Most things are not so much more difficult in newer versions -- the menus or buttons may change, but the internal behavior is exactly the same. There have been relatively few changes in macros and their behavior since Word 2003 except to add objects for new features in the user interface. Yes, there are some things that take more button clicks to get done, and some features that have been removed, but on the whole I don't find the newer versions to be more difficult, just different.
_____________________________
https://jay-freedman.info

8 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated May 9, 2024 Views 42,328 Applies to: