Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add Footer to Sections (Word 97)

    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

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Add Footer to Sections (Word 97)

    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

  3. #3
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Footer to Sections (Word 97)

    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 <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

    Thanks
    --Maria

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Add Footer to Sections (Word 97)

    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

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Add Footer to Sections (Word 97)

    Will this run?

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


  6. #6
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Footer to Sections (Word 97)

    Thank you so much! The procedure is much smaller and runs faster than the one I recorded <img src=/S/yep.gif border=0 alt=yep width=15 height=15>. I was able to add more stuff but have more questions. Will post later.

    Thanks
    --Maria

  7. #7
    JustCallMeAl
    Guest

    Re: Add Footer to Sections (Word 97)

    From the looks of your code, it appears you want the very same thing to appear in all the footers, except for the dynamic field values. Is that correct?

    If so, when you first create a document and add section breaks, the header/footers are all linked to previous. Therefore, you only need to insert the material you want to insert into the very first footer (or the last, if you are so inclined).

  8. #8
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Footer to Sections (Word 97)

    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

  9. #9
    2 Star Lounger
    Join Date
    Aug 2001
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Footer to Sections (Word 97)

    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:

    <pre>Sub Test()
    Dim intSection As Integer
    Selection.HomeKey Unit:=wdStory
    For intSection = ActiveDocument.Sections.Count To 1 Step -1
    With ActiveDocument.Sections(intSection).Footers(wdHead erFooterPrimary)
    .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

    </pre>



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

    --Maria

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Add Footer to Sections (Word 97)

    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.

    <pre> 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(wdHead erFooterPrimary)
    .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</pre>


  11. #11
    JustCallMeAl
    Guest

    Re: Add Footer to Sections (Word 97)

    Okay.

    Here is a little something about Footers in Word 97, along with code to "fix" the problem, that you may find interesting. The third of the For/Each is the code that performs the action we wanted to accomplish, namely, deleting a Time field code.

    <pre>'Take note that the developers of Word 97 seem to have made an _
    oversight whereby, although the Document Object's _
    StoryRanges Property's Item Method allows as arguments _
    wdEvenPagesFooterStory, wdFirstPageFooterStory, and _
    wdPrimaryFooterStory, none of these give access to the _
    primary footer in the second section of a document when _
    that footer's LinkToPrevious Property is False. The code _
    below, though, by iterating first through the Sections _
    Collection and then, within each of those, through the Footers _
    Collection, avoids this problem.

    Dim sct As Section
    Dim hfr As HeaderFooter
    Dim fie As Field


    For Each sct In ActiveDocument.Sections
    For Each hfr In sct.Footers
    For Each fie In hfr.Range.Fields
    If fie.Type = wdFieldTime Then _
    fie.Result.Paragraphs.First.Range.Delete
    Next
    Next
    Next
    </pre>


  12. #12
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Add Footer to Sections (Word 97)

    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  13. #13
    New Lounger
    Join Date
    Mar 2002
    Location
    Connecticut, USA
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Footer to Sections (Word 97)

    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.

  14. #14
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Add Footer to Sections (Word 97)

    Try adding the line

    <pre>rngTemp.Move Unit:=wdCharacter, Count:=-1
    </pre>


    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

  15. #15
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Leuven, Vlaanderen, Belgium
    Posts
    322
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Re: Add Footer to Sections (Word 97)

    StuartR,
    I encountered the same problem and your solution works!
    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    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.

Page 1 of 2 12 LastLast

Posting Permissions

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