Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Insert Fields Into Footer (W2K SR1)

    I would like to insert a number of fields into the footer in all sections of the current document, if possible without having to actually go an open each footer.

    There are actually about three fields with one of them having an If statement, eg

    {KEYWORDS}{IF {KEYWORDS} <> "" }{DOCPROPERTY "Category"}

    I can use the following code fragment to insert actual text into the footer, but can't work out how to modify it for this combination of fields:


    Dim sec

    For Each sec In ActiveDocument.Sections
    sec.Footers(wdHeaderFooterPrimary).Range.Text = "Footer"
    sec.Footers(wdHeaderFooterFirstPage).Range.Text = "First Page"
    Next sec

    It may be that I actually have to start at the beginning of the document and go into the first footer and use NextFooter and insert the fields this way. However, I don't think this is as foolproof or streamlined as modifying the above code fragment.

    Any suggestions would be appreciated. Thanks.

  2. #2
    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: Insert Fields Into Footer (W2K SR1)

    Here is a way to do it. You have to be careful not to wipe out the footer by using the footer range each time, hence the creation of a special range that works like an insertion point at the end of the footer range. Or you could use the Selection object, that would be simpler to understand.

    <pre>Sub InsertIntoFooter()
    Dim sec As Section, rngInsertHere As Range
    For Each sec In ActiveDocument.Sections
    'some sample text to start out
    sec.Footers(wdHeaderFooterPrimary).Range.InsertAft er "Page "
    'define an insertion point range
    Set rngInsertHere = sec.Footers(wdHeaderFooterPrimary).Range
    rngInsertHere.MoveStart wdCharacter, rngInsertHere.End - 1
    'insert a field
    ActiveDocument.Fields.Add rngInsertHere, wdFieldPage
    'some more sample text to prove we can InsertAfter the field
    sec.Footers(wdHeaderFooterPrimary).Range.InsertAft er "...whatever..."
    'redefine the insertion point range
    Set rngInsertHere = sec.Footers(wdHeaderFooterPrimary).Range
    rngInsertHere.MoveStart wdCharacter, rngInsertHere.End - 1
    'insert another field
    ActiveDocument.Fields.Add rngInsertHere, wdFieldPage
    Next sec
    End Sub</pre>

    (I didn't try to insert KEYWORDS fields, since I don't know anything about them.)

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Insert Fields Into Footer (W2K SR1)

    Thank you very much for your help. I've played around with it so I think I (almost) understand how it all works and have modified it for myself.

    The only thing I can't work out how to do is how to create an IF statement with a field code as part of its comparison test. I want to say if the keywords property is not empty insert a (backslash). I can do this easily with field codes, but cannot work out how to code it.

    The following is what I have so far:


    Sub InsertIntoFooter2()
    Dim sec As Section, rngInsertHere As Range
    For Each sec In ActiveDocument.Sections
    Set rngInsertHere = sec.Footers(wdHeaderFooterPrimary).Range

    'by not using the following line the text in the footer is replaced
    ' rngInsertHere.MoveStart wdCharacter, rngInsertHere.End - 1

    'insert a field
    ActiveDocument.Fields.Add rngInsertHere, Type:=wdFieldKeyWord, _
    Preserveformatting:=False

    'move to the end of that field
    Set rngInsertHere = sec.Footers(wdHeaderFooterPrimary).Range
    rngInsertHere.MoveStart wdCharacter, rngInsertHere.End - 1

    'insert an If field
    'The following line inserts the If field but not the keywords field
    ActiveDocument.Fields.Add rngInsertHere, Type:=wdFieldIf, _
    Preserveformatting:=False, Text:=" <> """" """""

    'move to the end of that field
    Set rngInsertHere = sec.Footers(wdHeaderFooterPrimary).Range
    rngInsertHere.MoveStart wdCharacter, rngInsertHere.End - 1

    'insert a field
    ActiveDocument.Fields.Add rngInsertHere, Type:=wdFieldDocProperty, _
    Preserveformatting:=False, Text:="Category"

    Next sec
    End Sub

    The field should end up looking like this:
    {IF {KEYWORDS} <> "" ""}
    I can get {IF <> "" "" }

    Any ideas?

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

    Re: Insert Fields Into Footer (W2K SR1)

    Karen

    If you can't work out the correct syntax to next the fields, I would recommend you use an autotext entry. Create the real thing in word and then select it and add to autotext. Then use the macro to insert that autotext entry in the correct spot.

    This is a great method for adding non-codeable content such as graphics/logos but it should also do the job for this task.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  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: Insert Fields Into Footer (W2K SR1)

    I don't see any way to do it using the Range approach. MS has a programming example where they flip to viewing field codes, march the insertion point into the field, and then insert fields using the Selection object: <A target="_blank" HREF=http://support.microsoft.com/support/kb/articles/Q209/2/20.ASP>Q209220

Posting Permissions

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