none
Insert footer based on header selected RRS feed

  • Question

  • can anyone point me in the right direction as to how I can add a footer building block based on the header building block selected?? We have templates for all our city locations. the content of the templates are identical for all cities and only the headers and footers are different. I created the building blocks for the header and footers  and I want to create 1 template that ALL cities can use simply by adding their local city header/footer. It works as it is now however its a 2 step process(add header...then add footer) that I would like to change to 1 step

    TIA

    WP
    Wednesday, August 13, 2014 5:28 PM

All replies

  • If you setup the template and the building blocks suitably it is relatively easy to select the required headers and footers using AutoText fields.

    Save or edit the autotext entries for the headers and footers to name them e.g.

    LondonHeader
    LondonFooter
    NewYorkHeader
    NewYorkFooter
    DenverHeader
    DenverFooter

    etc

    Insert an AUTOTEXT field into the header

    {AUTOTEXT LondonHeader}

    and another in the footer

    {AUTOTEXT LondonFooter}

    (It doesn't matter which of your cities you chose, but the case is important.

    Add the following code to a new module in the template. Change the section indicated in bold

    Case strCity = "London", _
                 strCity = "NewYork", _
                 strCity = "Denver"

    to reflect the cities in your organisation as used in the autotext names, i.e. case sensitive and no spaces.When you create a new document, the AutoNew macro will run to run the GetLogos macro which will prompt for the city. The input box is not case sensitive

    It may be possible to determine the city automatically, but you have not provided sufficient information to establish the potential of that.

    The procedure ChangeHeaderFooter allows for templates with different first page headers and subsequent page headers if required.

    Option Explicit

    Sub ChangeHeaderFooter(strCity As String, bFooter As Boolean, Optional Primary As Boolean)
    Dim oFld As Field
    Dim oRng As Word.Range
        If Not ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True Then
            Primary = True
        End If
        If Primary = True Then
            If bFooter = True Then
                Set oRng = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
            Else
                Set oRng = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
            End If
        Else
            If bFooter = True Then
                Set oRng = ActiveDocument.StoryRanges(wdFirstPageFooterStory)
            Else
                Set oRng = ActiveDocument.StoryRanges(wdFirstPageHeaderStory)
            End If
        End If
        For Each oFld In oRng.Fields
            If oFld.Type = wdFieldAutoText Then
                oFld.Locked = False
                oFld.Code.Text = Replace(oFld.Code, oFld.Code.Text, "AUTOTEXT " & strCity)
                oFld.Update
                oFld.Locked = True
                Exit For
            End If
        Next oFld
    End Function

    Sub AutoNew()
        GetLogos
    End Sub

    Sub GetLogos()
    Dim strCity As String
        strCity = StrConv(InputBox("Enter the City"), vbProperCase)
        strCity = Trim(Replace(strCity, Chr(32), ""))
        Select Case True
            Case strCity = "London", _
                 strCity = "NewYork", _
                 strCity = "Denver"
                ChangeHeaderFooter strCity & "Header", False
                ChangeHeaderFooter strCity & "Footer", True
            Case Else
                MsgBox "City header/footer not available"
        End Select
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Thursday, August 14, 2014 8:02 AM
  • Hi Graham

    I keep on getting City/Header not available.

    I have saved the building blocks as autotext in both the template and building blocks file and got the same issue..Unsure of what I may be doing wrong!!

    I put {AUTOTEXT TorontoHeader} in the header and {AUTOTEXT TorontoFooter} in the footer

    Also...the form asking for city doesn't pop up when the template is opened

    Option Explicit

     Sub ChangeHeaderFooter(strCity As String, bFooter As Boolean, Optional Primary As Boolean)
     Dim oFld As Field
     Dim oRng As Word.Range
         If Not ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True Then
             Primary = True
         End If
         If Primary = True Then
             If bFooter = True Then
                 Set oRng = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
             Else
                 Set oRng = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
             End If
         Else
             If bFooter = True Then
                 Set oRng = ActiveDocument.StoryRanges(wdFirstPageFooterStory)
             Else
                 Set oRng = ActiveDocument.StoryRanges(wdFirstPageHeaderStory)
             End If
         End If
         For Each oFld In oRng.Fields
             If oFld.Type = wdFieldAutoText Then
                 oFld.Locked = False
                 oFld.Code.Text = Replace(oFld.Code, oFld.Code.Text, "AUTOTEXT " & strCity)
                 oFld.Update
                 oFld.Locked = True
                 Exit For
             End If
         Next oFld
     End Function

     Sub AutoNew()
         GetLogos
     End Sub

    Sub GetLogos()
     Dim strCity As String
         strCity = StrConv(InputBox("Enter the City"), vbProperCase)
         strCity = Trim(Replace(strCity, Chr(32), ""))
         Select Case True
             Case strCity = "TorontoHeader", _
                 strCity = "OttawaHeader", _
                 strCity = "LondonHeader", _
                 strCity = "EdmontonHeader", _
                 strCity = "CalgaryHeader", _
                 strCity = "KelownaHeader", _
                 strCity = "VancouverHeader", _
                 strCity = "Victoria"
                 ChangeHeaderFooter strCity & "Header", False
                 ChangeHeaderFooter strCity & "Footer", True
             Case Else
                 MsgBox "City header/footer not available"
         End Select
     End Sub


    • Edited by WPSA Friday, August 15, 2014 12:08 PM
    Friday, August 15, 2014 12:04 PM
  • There are a couple of obvious issues (one of them my typo). Addressing the typo first, the 'End Function' at the end of the ChangeHeaderFooter macro should be 'End Sub', but compiling the code should have revealed that.

    The reason the headers are not found is that the macro 'GetLogos' should read as follows. ie. Don't include 'Header' in the case statements or the macro will be looking for an autotext entry called e.g. TotontoHeaderHeader.

    The input box does not open when you open the template/document. It opens when you create a new document from the template File > New. Type only the city name in the box.

    Ideally you should use a userform with a list box or combo box to select the cities and thus avoid typing errors, but userforms are difficult to reproduce in the forum environment. See http://www.gmayor.com/Userform.htm  which should helo get you started with that.

    The fields in the header/footer must be true fields i.e. the brackets '{ }' must be inserted with CTRL+F9.

    Sub GetLogos()
     Dim strCity As String
         strCity = StrConv(InputBox("Enter the City"), vbProperCase)
         strCity = Trim(Replace(strCity, Chr(32), ""))
         Select Case True
             Case strCity = "Toronto", _
                 strCity = "Ottawa", _
                 strCity = "London", _
                 strCity = "Edmonton", _
                 strCity = "Calgary", _
                 strCity = "Kelowna", _
                 strCity = "Vancouver", _
                 strCity = "Victoria"
                 ChangeHeaderFooter strCity & "Header", False
                 ChangeHeaderFooter strCity & "Footer", True
             Case Else
                 MsgBox "City header/footer not available"
         End Select
     End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, August 16, 2014 1:33 PM
  • Further to my last, you can see a working example, with a userform at https://dl.dropboxusercontent.com/u/57986755/Example%20Template.dotm


    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, August 16, 2014 2:11 PM
  • Thanks Graham

    This is exactly what we are looking to do....great job!!!

    Tuesday, August 19, 2014 11:00 AM