none
Can't Get Auto Header/Footer to Work. RRS feed

  • Question

  • I use to have a macro in Word 2003 that would add a footer and header from the second page forward. But I have never been able to recreate a workable macro in Word 2007. It always errors out like this:

    Run-time error '5941':

    The requested member of the collection does not exist.

    My macro was always based on the assumption that the document already had a saved file name of choice which would become the header. The footer would just be one of the standard footers that comes with Word. Here is the macro code that always errors out:

    Sub AutoHeadFoot()
    '
    ' AutoHeadFoot Macro
    ' Apply headers and footers to a saved document.
    '
        WordBasic.ViewFooterOnly
        WordBasic.ViewFooterOnly
        ActiveDocument.AttachedTemplate.BuildingBlockEntries("Conservative"). _
            Insert Where:=Selection.Range, RichText:=True
        WordBasic.GoToHeader
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "FILENAME  \* Caps ", PreserveFormatting:=True
        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        ActiveDocument.Save
    End Sub

    Any suggestions?

    Thursday, September 8, 2011 5:45 PM

Answers

  • Hi vicStuart,

    Please change the "Conservative" into the number, then this error will not occur. For example, "Conservative" is the first item in BuildingBlockEntries collection. The you could modify the code like below:

        WordBasic.ViewFooterOnly
    
        ActiveDocument.AttachedTemplate.BuildingBlockEntries(1). _
    
            Insert Where:=Selection.Range, RichText:=True
    
    

    Please have a try and let us know the situation on your side.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Liliane Teng Thursday, September 15, 2011 9:42 AM
    • Marked as answer by Liliane Teng Sunday, September 18, 2011 10:59 AM
    Thursday, September 15, 2011 9:39 AM

All replies

  • Is the text saved as AutoText? If so substitute "AutoTextEntries" for "BuildingBlockEntries".
    Kind Regards, Rich ... http://greatcirclelearning.com
    Friday, September 9, 2011 12:49 AM
  • Hi vicStuart,

    Please change the "Conservative" into the number, then this error will not occur. For example, "Conservative" is the first item in BuildingBlockEntries collection. The you could modify the code like below:

        WordBasic.ViewFooterOnly
    
        ActiveDocument.AttachedTemplate.BuildingBlockEntries(1). _
    
            Insert Where:=Selection.Range, RichText:=True
    
    

    Please have a try and let us know the situation on your side.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Edited by Liliane Teng Thursday, September 15, 2011 9:42 AM
    • Marked as answer by Liliane Teng Sunday, September 18, 2011 10:59 AM
    Thursday, September 15, 2011 9:39 AM