Share via

Run Time error 5941

Anonymous
2019-01-07T16:21:07+00:00

I've got a template with a macro that loads a Building Block. Worked fine for years, then I changed the location of the template and now I'm getting run time errors. There are several blocks of text like this one that I use that are basically like this one.

Sub InsertRevLog()

' Insert Revision Log

Application.Templates.LoadBuildingBlocks

        "\\Wg16\srtemplate\Procedure_Template.dotm"). _

        BuildingBlockEntries("RevLog").Insert Where:=Selection.Range, RichText:= _

        True

End Sub

I've read other places I could add this line:

Application.Templates.LoadBuildingBlocks

That didn't seem to work, though. The error is hitting on the location line, which is good. I'm out of ideas.

Thanks for any help!

April

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2019-01-14T13:10:09+00:00

In the end, I saved the template to an alternate location, then moved it over to the correct location from there. That reset the location of the file itself and made the links in the code correct again.

Thanks for your help!

April

Was this answer helpful?

0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Charles Kenyon 167.7K Reputation points Volunteer Moderator
    2019-01-09T01:30:15+00:00

    Its a building block, which shows up in the Building Block Organizer. Would those have been jettisoned from the existing template by moving it?

    That it shows up in the building blocks organizer means that it is in this template, the attached template, or a loaded global, or a building blocks file.

    This would be simplest if the building blocks are stored in the same template as the one holding your code. That way you could move it with impunity.

    Where can Building Blocks be stored?

    Was this answer helpful?

    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2019-01-09T01:02:10+00:00

    If you simply moved the file from one folder path to another, the building blocks would not be removed.

    The code as you posted it won't work; it should get a compile error because the beginning of the statement that now starts with the first double quote is missing. If what's missing is

    Templates(

    then the macro still won't work. The problem is this: When Word starts up, the Templates collection contains only Normal.dotm plus any template files that are in the STARTUP folder. Running the LoadBuildingBlocks command adds any template files that are in the Document Building Blocks folder under the user's profile (%appdata%\Microsoft\Document Building Blocks). The collection still won't include any template from any other folder, let alone from a share on another computer. The attempt to access

    Templates("\Wg16\srtemplate\Procedure_Template.dotm")

    will cause the error 5941, "The requested member of the collection does not exist", referring to the Templates collection.

    One solution is to base the document on Procedure_Template.dotm, or to include the RevLog building block in whatever other template on which you do base the document.

    If neither of those will work for you, the last solution is to add code to the macro to load the Procedure_Template.dotm as an add-in (so that template is a member of the Templates collection) before you force load the building blocks. The quoted string in the Templates() reference will still need the full path to the file.

    Sub InsertRevLog()

    ' Insert Revision Log

    AddIns.Add FileName:="\Wg16\srtemplate\Procedure_Template.dotm", Install:=True

    Application.Templates.LoadBuildingBlocks

    Templates("\Wg16\srtemplate\Procedure_Template.dotm"). _

            BuildingBlockEntries("RevLog").Insert Where:=Selection.Range, _

            RichText:=True

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-01-08T16:30:59+00:00

    Its a building block, which shows up in the Building Block Organizer. Would those have been jettisoned wfom the existing template by moving it?

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2019-01-07T23:33:13+00:00

    The issue is that Word is trying to obtain something from either a location that no longer exists, or that is not in the location in which the code is expecting it to be, probably as a result of your relocation of the template.

    Was this answer helpful?

    0 comments No comments