• Run-time error 5941 in Word 2010

    Author
    Topic
    #490280

    As I cannot write VBA code, I recorded a macro to, amongst other things, insert a page number in a footer of a document, using ‘Simple Number 3’. This has resulted in VBA code containing:

    Application.Templates( _
    “C:UsersDaleAppDataRoamingMicrosoftDocum ent Building Blocks103314Built-In Building Blocks.dotx” _
    ).BuildingBlockEntries(“Plain Number 3”).Insert Where:=Selection.Range, _
    RichText:=True
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    When I run this macro, it crashes with:

    Run-time error 5941
    The requested member of the collection does not exist.

    I have checked the .dotx file it refers to and it is there and the Building Block ‘Simple Number 3’ does exist, so the error must be caused by something else.

    In the searching I have done prior to posting, I have come across a suggestion that:

    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.

    The suggestion is to add after the comment lines at the beginning:

    Application.Templates.LoadBuildingBlocks

    In my case, adding this as suggested does NOT work.

    The code also raises another critical issue.

    This template is on the file server and is intended to be accessed by a number of people on the network. If the path is pointing to a specific user’s local C: drive, how would this work for someone else in any event?

    I would be grateful for advice on how to fix the error and how to overcome the user specific path issue.

    Regards
    useful

    Viewing 2 reply threads
    Author
    Replies
    • #1403686

      The macro recorder is not to be trusted to record macros that actually run. :flee: I wrote an article, http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm, that barely scratches the surface.

      In this case, the problem is that the Templates collection (unlike some other collections) can’t use a file name as the index; in an expression like Templates(something) the “something” must be an integer.

      After you run the statement Application.Templates.LoadBuildingBlocks, the Built-In Building Blocks.dotx template should be Templates(1). Your code can check that by looking at Templates(1).Name to see whether it equals “Built-In Building Blocks.dotx”.

      That also solves the problem of the path pointing to the user’s drive, because you don’t have to refer to it — the LoadBuildingBlocks method knows where to look.

      BUT: Don’t do any of that! You should not use a macro to insert a built-in building block in a document based on a specific (not Normal.dotm) template. Instead, just stick the desired formatted page number into the footer of the template itself, and it will always appear automatically in every new document that anyone bases on that template.

      • #1403727

        Thanks for the response.

        I clearly have to change the way I insert a page number into the footer, but as it is on the second page that does not exist until the macro is run, I’m not quite sure how to do it.

        Regards
        useful

        • #1403746

          I clearly have to change the way I insert a page number into the footer, but as it is on the second page that does not exist until the macro is run, I’m not quite sure how to do it.

          In the body of the template, insert a manual page break (Ctrl+Enter) so you can see page 2. This is temporary; you’ll remove it later.

          Open the header or footer pane, so you can see the Header & Footer Tools tab. On that tab, check the box for Different First Page.

          In the footer of page 2, insert the page number where you want it.

          Close the footer pane, and delete the manual page break. The template will now have only one page, with nothing showing in the footer, but it will “remember” the second (and later) page footer and will display it when a document grows longer than one page — whether because of a macro or because of manual editing.

          Save the template.

          The same setup can apply to the header, if it needs to be different on the first page than in the rest of the document.

          NOTE: Once you see how this works, you can save a couple of steps. You can start in the one-page template, create the second-page footer on page 1, and then turn on Different First Page. The footer will disappear, because now you’re looking at the First Page Footer; but the second-page footer will still be in the file and will appear when it’s needed.

      • #1403740

        Thank you!

        I have worked out another way to insert the page number, so I don’t need the Built-In Building Blocks.dotx template, thus solving both of my problems.

        Regards
        useful

    • #1403703

      Your code is pointing at “Plain Number 3” and you checked for the existence of “Simple Number 3”?

      The code should reside in the same template as the Building Block. The ‘Built-in’ template is local to the current user so it is not the place to put the building block. I put the building block in the attached template if that is where the code is run from.

      ActiveDocument.AttachedTemplate.BuildingBlockEntries(“Plain Number 3”).Insert Where:=Selection.Range, RichText:=True

      • #1403713

        Hi Andrew

        Thanks for picking this up, but that was a typo in my post. The code does correctly refer to ‘Plain Number 3’ as contained in Built-In Building Blocks.dotx. I will change the way I insert page number, so that it doesn’t rely on the use of this file.

        Regards
        useful

    • #1403749

      Thank you.

      Regards
      useful

    Viewing 2 reply threads
    Reply To: Run-time error 5941 in Word 2010

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: