• Add Footer to Sections (Word 97)

    Author
    Topic
    #359623

    Hello:

    I am trying to create a macro that will add Footers to each section of my document. I have 4 sections on a sample document I am working on and after I run the code below, Footer on Section 1 says “Section 2”, Footer on Section 2 says “Section 3”, Footer on Section 3 says “Section 4” then Footer on Section 4 says “Section 1″. What am I doing wrong? All I want is for the Section Number to appear inside each Footer.

    Sub CreateFooter()
    Dim SectionCount, x
    SectionCount = 0
    x = 0

    ‘getting Section count
    SectionCount = ActiveDocument.Sections.Count

    ‘adding Footer to each Section
    For x = 1 To SectionCount
    Selection.GoTo What:=wdGoToSection, Count:=x
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.HeaderFooter.LinkToPrevious = False
    Selection.WholeStory
    Selection.delete Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=”Seciton ” & x
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Next
    Selection.HomeKey Unit:=wdStory
    End Sub

    Thanks,
    Maria

    Viewing 2 reply threads
    Author
    Replies
    • #539525

      Hi Maria,

      The problem appears to lie in the method you’re using to move from section to section – this doesn’t appear to work as you might think.

      Recording a macro of going to a section identified by number nets the following piece of code:

      Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=x
      (well, I’ve put the “x” in there now).

      Substitute the above line for the line:
      Selection.GoTo What:=wdGoToSection, Count:=x

      and the macro seems to run fine.

      By the way, a couple of points about the variable declarations at the top:
      You ought to assign a variable type at the same time that you declare a variable, for example:

      Dim SectionCount As Integer, x as Integer

      Also, you don’t need to explicitly set these newly declared variables to zero after you’ve declared them – basically, newly-declared variables are empty until they get a value assigned to them in the course of the code, so for instance an Integer or Long will automatically evaluate to 0 ‘at birth’.

      Hope this helps,
      Gary

      • #539540

        Hi Gary:

        I did what you suggested and it did not fix the problem. Section for is still saying “Section 1” in the footer and Section 1 is still saying “Section 2”

        I am new to this hairout

        Thanks
        –Maria

        • #539549

          Hi again,

          It’s working fine here so I’m a bit stumped – I am working on Word 2000 (and don’t have access to Word 97 anymore to test this), so don’t know if this happens to work differently between Word 97 and Word 2000.

          Does it make a difference where in the document you are when you run the macro? What happens if you add:

          Selection.HomeKey Unit:=wdStory

          at the beginning of the macro?

          Gary

        • #539566

          Will this run?

          Sub NumberFooters()
          Dim intSection As Integer
          For intSection = ActiveDocument.Sections.Count To 1 Step -1
              With ActiveDocument.Sections(intSection).Footers(wdHeaderFooterPrimary)
                  .Range.Delete
                  .LinkToPrevious = False 'works in SR-1a and higher only for 2000 (bug!)
                  .Range.InsertAfter "Section Numero " & intSection
              End With
          Next
          End Sub
          • #539745

            Thank you so much! The procedure is much smaller and runs faster than the one I recorded yep. I was able to add more stuff but have more questions. Will post later.

            Thanks
            –Maria

            • #539879

              Hi Jeff:

              I tried adding several fields in the footer but couldn’t do it w/o removing previously added fields. What I would like is for the following to show up:

              {FILENAME}
              Page {PAGES} OF {NUMPAGES]
              Section intSection

              Here’s the code:

              Sub Test()
                  Dim intSection As Integer
                   Selection.HomeKey Unit:=wdStory
                  For intSection = ActiveDocument.Sections.Count To 1 Step -1
                      With ActiveDocument.Sections(intSection).Footers(wdHeaderFooterPrimary)
                          .Range.delete
                          .LinkToPrevious = False 'works in SR-1a and higher only for 2000 (bug!)
                          
                          'insert filename field
                          .Range.Fields.Add Range:=.Range, Type:=wdFieldFileName, PreserveFormatting:=True
                          
                          'insert Page X of Y
                          .Range.InsertParagraphAfter
                          .Range.InsertAfter "Page "
                          .Range.Fields.Add Range:=.Range, Type:=wdFieldPage, _
                             Text:="* ALPHABETIC", PreserveFormatting:=True
                          .Range.InsertAfter "of "
                          .Range.Fields.Add Range:=.Range, Type:=wdFieldNumPages, _
                             Text:="* ALPHABETIC", PreserveFormatting:=True
                          .Range.InsertParagraphAfter
                          .Range.InsertAfter "Section No. " & intSection
                      End With
                      Next
              
                      Selection.HomeKey Unit:=wdStory
                      ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
                      With Selection.HeaderFooter.PageNumbers
                          .RestartNumberingAtSection = False
                          .StartingNumber = 3
                      End With
                      ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
              End Sub
              
              

              Thank you so much for your help. I do appreciate them.

              –Maria

            • #539947

              Yes, every time a field is inserted to the Range, it replaces the entire thing. So, rather than insert to the Range that constitutes the footer, we need to create a new range that constitutes the point after everything else in the footer. There was a discussion of this in another thread where I confessed that I really didn’t know how to do it. Let me see if I can figure it out.

              This is one way, creating a second range that points to the end of the first range, but there may be more efficient ways. It might even be possible to collapse the footnote range itself, but that seemed sort of risky so I didn’t go there.

                  Dim intSection As Integer, aTempRange As Range
                  Selection.HomeKey Unit:=wdStory
                  For intSection = ActiveDocument.Sections.Count To 1 Step -1
                      With ActiveDocument.Sections(intSection).Footers(wdHeaderFooterPrimary)
                          .Range.Delete
                          .LinkToPrevious = False 'works in SR-1a and higher only for 2000 (bug!)
                          
                          'insert filename field
                          .Range.Fields.Add Range:=.Range, Type:=wdFieldFileName, PreserveFormatting:=True
               
                          'insert Page X of Y
                          .Range.InsertParagraphAfter
                          .Range.InsertAfter "Page "
               
                          Set aTempRange = .Range
                          aTempRange.Collapse wdCollapseEnd
                          .Range.Fields.Add Range:=aTempRange, Type:=wdFieldPage, _
                             Text:="* ALPHABETIC", PreserveFormatting:=True
                          .Range.InsertAfter " of "
               
                          Set aTempRange = .Range
                          aTempRange.Collapse wdCollapseEnd
                          .Range.Fields.Add Range:=aTempRange, Type:=wdFieldNumPages, _
                             Text:="* ALPHABETIC", PreserveFormatting:=True
                          .Range.InsertParagraphAfter
                          .Range.InsertAfter "Section No. " & intSection
                          Set aTempRange = Nothing
                      End With
                  Next
            • #578714

              Hi,

              I just tried this technique, but it failed with

              Run Time Error 4605 – This command is not available

              Here is the code

              ….
              With .Cells(3)
              .Range.InsertAfter “Page ”
              Dim rngTemp
              Set rngTemp = .Range
              rngTemp.collapse wdCollapseEnd
              ‘***Error is on next line!!
              .Range.Fields.Add rngTemp, Type:=wdFieldPage, PreserveFormatting:=False
              .Range.InsertAfter ” of ”
              Set rngTemp = .Range
              rngTemp.collapse wdCollapseEnd
              .Range.Fields.Add rngTemp, Type:=wdFieldNumPages, PreserveFormatting:=False
              End With

              Any ideas?

              I’m in a table in a footer. Perhaps related to the table?

              I’m trying to add the field without resorting to a Selection so any help would be appreciated.

            • #578717

              Try adding the line

              rngTemp.Move Unit:=wdCharacter, Count:=-1
              

              Just before the line that gives the error. I think that the range has been collapsed to just the Cell marker at the end of the table cell and you can’t put a field in it!

              StuartR

            • #632021

              StuartR,
              I encountered the same problem and your solution works!
              thankyou
              I should never never never have figured this out myself (*).
              Thanks!
              Hans, Belgium

              (*) I found your post ‘too late’ after I posted (197819) a new thread on this matter (“Can’t add field to table cell (Word 97)”). So I quoted your post over there, hoping that I did it right & didn’t ‘mess up’ threads too much like this.

    • #539894

      Hi Al:

      No, I don’t want every footer to be the same that is why LINKTOPREVIOUS is set to FALSE. At the end of each footer, the Section number is added.

      Thanks

      –Maria

      • #539987

        Maria

        If you used a field to hold the Section Number then you could still link to previous for all your header and footers.
        {Section} will show the current section number
        {StyleRef “Heading 1” n} will show the number of the last used heading.

        Depending on how you have set up your document, one of these may do the job you require without needing different headers and footers in each section. The second option even removes the need for section breaks if you don’t want to restart the page numbering.

    • #933933

      Hey Al

      this is neat code. many thanks for that

      diana 🙂

    Viewing 2 reply threads
    Reply To: Add Footer to Sections (Word 97)

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

    Your information: