Results 1 to 10 of 10
  1. #1
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Add Autotext to Footer (Word 2003 SP2)

    I know how to do this systematically opening and closing footers through the document, but I wonder if there's cleaner way to do this.

    Here's the code I currently have:
    <pre>Sub AddMissingDocIDTextBox()
    Dim sec As Section
    Dim ftr As HeaderFooter
    For Each sec In ActiveDocument.Sections
    For Each ftr In sec.Footers
    If ftr.Exists Then
    ActiveDocument.AttachedTemplate.AutoTextEntries("D ocID").Insert _
    where:=ftr.Range, RichText:=True
    End If
    Next ftr
    Next sec
    </pre>

    Unfortunately, it replaces the footer range with the autotext. Is there a way to add the autotext to the current contents of the footer using this or a similar method? I use slightly different code to fiddle with the textbox in the footer once it's there, but I can't seem to get it there without having to recreate the existing footer.

    Thanks! <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>
    Kim

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

    Re: Add Autotext to Footer (Word 2003 SP2)

    Try this modification
    <pre>Sub AddMissingDocIDTextBox()
    Dim sec As Section, aRange As Range
    Dim ftr As HeaderFooter
    For Each sec In ActiveDocument.Sections
    For Each ftr In sec.Footers
    If ftr.Exists Then
    Set aRange = ftr.Range
    aRange.Start = aRange.End
    ActiveDocument.AttachedTemplate.AutoTextEntries("D ocID").Insert _
    where:=aRange, RichText:=True
    End If
    Next ftr
    Next sec
    End Sub</pre>

    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Add Autotext to Footer (Word 2003 SP2)

    Andrew!

    That worked perfectly! Is this like "collapsing" the Range? I just want to understand...

    Thanks so much... <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

    Kim

  4. #4
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Add Autotext to Footer (Word 2003 SP2)

    Andrew,

    I do have a follow-up question, if you happen to know. I just noticed that it inserted the AutoText into my footers twice. I have a two-page test document with one section, but different first page footer. I didn't see it at first at the boxes were on top of each other.

    I know I could test the footer to see if the text box is there (I gave the shape a name) -- and I probably should as someone could run this code without checking. But if you have any insight, I'd appreciate it.

    Thanks again,
    Kim

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Add Autotext to Footer (Word 2003 SP2)

    Are you sure you haven't run the code twice accidentally? In an - addmittedly quick - test I did, Andrew's code added shapes only once.

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

    Re: Add Autotext to Footer (Word 2003 SP2)

    I think that this will add the AutoText multiple times if your document has multiple sections with the footers of one section Linked to the previous one.

    Try replacing the line
    <code> If ftr.Exists Then</code>
    with
    <code> If ftr.Exists And Not ftr.LinkToPrevious Then</code>

    StuartR

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Add Autotext to Footer (Word 2003 SP2)

    Excellent point, but Kim stated that the document where the problem occurred had only one section.

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

    Re: Add Autotext to Footer (Word 2003 SP2)

    This may have occurred because you ran the macro twice or possibly because of the way that Word handles floating objects in headers and footers.

    If you can eliminate the first possibility then have a look at <post:=157,050>post 157,050</post:> and consider whether it might be related to the floating object and the multiple footers you apply this floating object to.

    Without seeing your DocID content it is pretty hard to work out what the problem is - I don't suppose you checked whether the Autotext entry actually contained two items?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

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

    Re: Add Autotext to Footer (Word 2003 SP2)

    This was simply collapsing the range. You were inserting the content over the top of the 'selected' range and thus replacing it. I changed it so you collapsed the range to the end of the footer and then your inserted content gets added to the end.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Add Autotext to Footer (Word 2003 SP2)

    Thanks so much for this, Andrew, Stuart and Hans!

    I had a two-page document with a different first page footer. I ran the macro again this evening with no adjustments - and it inserted only one instance of the autotext per footer. Hmmmm.

    While this is very good news, it confounds me somewhat since I stepped through the code last night using F8 when it was inserted twice. I didn't think I accidentally ran it twice. When I looked in the Undo history it had four instances of Insert AutoText.... which would be true whether it was malfunctioning or I was.

    As it stands, it looks as if *my* code needs adjusting. This is good news as I can do that with a nice glass of wine and a hot bath.

    Thanks again, <img src=/S/hugs.gif border=0 alt=hugs width=41 height=25>
    Kim

Posting Permissions

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