Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts

    Run-time error 5941 in Word 2010

    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:\Users\Dale\AppData\Roaming\Microsoft\Docum ent Building Blocks\1033\14\Built-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

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    The macro recorder is not to be trusted to record macros that actually run. I wrote an article, http://www.word.mvps.org/FAQs/Macros...ordedMacro.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.

  3. The Following User Says Thank You to jjfreedman For This Useful Post:

    useful (2013-07-29)

  4. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    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.BuildingBlockEntri es("Plain Number 3").Insert Where:=Selection.Range, RichText:=True
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #4
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    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

  6. #5
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    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

  7. #6
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    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

  8. #7
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Quote Originally Posted by useful View Post
    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.
    Last edited by jjfreedman; 2013-07-29 at 21:43. Reason: added info

  9. The Following User Says Thank You to jjfreedman For This Useful Post:

    useful (2013-07-29)

  10. #8
    3 Star Lounger
    Join Date
    May 2002
    Location
    Melbourne, Australia
    Posts
    269
    Thanks
    32
    Thanked 0 Times in 0 Posts
    Thank you.

    Regards
    useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •