Results 1 to 13 of 13
  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

    textbox in footer again (Word 2003 SP2)

    In my ongoing saga of the textbox in the footers, I have run into another wall.

    While I now think I understand much more about headers and footers than I used to, I still can't bend them to my will.

    Using the attached four-page document and the code below, here's my problem:

    I want to make sure there's a textbox named "DocID" on every page, regardless of how the user has set up their footers. The attached document has a first page footer with nothing in it. The second page has a page number and a DocID box. The second section is identical to the first because they are linked to previous.

    When I walk through this code:
    <pre>Dim sec As Section
    Dim ftr As HeaderFooter
    Dim tbox As Shape
    Dim tboxFound As Boolean
    Dim rng As Range

    For Each sec In ActiveDocument.Sections
    For Each ftr In sec.Footers
    tboxFound = False
    If ftr.Exists And Not ftr.LinkToPrevious Then
    For Each tbox In ftr.Shapes
    If tbox.Name = "DocID" Then
    Dim ftrType
    ftrType = ftr.Range.Information(wdHeaderFooterType)
    MsgBox ftrType, vbOKOnly
    If blivit = 3 Then
    tbox.Line.Visible = msoTrue
    ElseIf ftrType = 5 Then
    tbox.Line.Visible = msoTrue
    tbox.Line.DashStyle = msoLineDashDotDot
    End If
    tboxFound = True
    End If
    Next tbox
    If tboxFound = False Then
    Set rng = ftr.Range
    rng.Start = rng.End
    ActiveDocument.AttachedTemplate.AutoTextEntries("D ocID").Insert _
    where:=rng, RichText:=True
    End If
    End If
    Next ftr
    Next sec
    </pre>


    The code finds the same textbox twice - the first time it reports "3" - Odd page (or only) footer. The second time it reports "5" - First page footer. The problem is that it finds the textbox and I can't trigger the code to insert the AutoText.

    When there is a textbox on page one, the code cycles through four times and reports "3" for each of the textboxes, then "5" for each.

    So, given what I want to do, and given how Word is operating, I assume I'm going to have to use a different approach. How can I make sure there's a textbox on every page regardless of how the footers are handled? And how can I skip a footer if the box is already there?

    If this keeps up, I won't have any hair left... <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>
    Thanks!
    Kim
    Attached Files Attached Files

  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: textbox in footer again (Word 2003 SP2)

    Okay, try this:

    <code>If ftr.Exists And Not ftr.LinkToPrevious And ftr.Range.ShapeRange.Count > 0 Then
    For Each tbox In ftr.Range.ShapeRange</code>

    This seems more targeted than the ftr.Shapes collection.

  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: textbox in footer again (Word 2003 SP2)

    (I was prepping this when I got your second message. ShapeRange didn't work either. It still finds the textbox on page two and therefore doesn't insert the AutoText on the first page. I keep thinking there *has* to be a way...! But here's my original response.)

    Dang! I thought I replaced all the "blivit"s with "ftrType". Blivit was a word my dad used and it's meaning would change at his whim. I suspect it started out as something rude, but you could never pin him down on it.

    I used to use a frame for this until I found that textboxes had names. Now that I see they have multiple personalities (at least when living in footers), they won't do me much good. Maybe there's some other property of a frame that I can use to distinguish my DocID frame - like a pattern that's not visible because it's white on white... or something. I'll poke around.

    No wonder I haven't been able to find anything about this anywhere...

    Thanks much!
    Kim

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

    Re: textbox in footer again (Word 2003 SP2)

    Why didn't the Visual Basic Editor warn you that "blivit" wasn't defined? You do have Require Variable Declaration turned on in Tools | Options..., don't you? If not, you should
    1) Turn it on immediately. From now on, the VBE will add a line Option Explicit at the top of all new modules you create.
    2) Insert a line Option Explicit at the top of all existing modules.

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

    Re: textbox in footer again (Word 2003 SP2)

    Floating shapes in headers and footers are hard to work with because all headers and footers in the entire document share one collection of shapes. See <post:=167,988>post 167,988</post:> and <post:=157,050>post 157,050</post:> for examples of the complicated code you need to deal with this. It's slighlty easier if you use inline shapes.

  6. #6
    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: textbox in footer again (Word 2003 SP2)

    > ShapeRange didn't work either. It still finds the textbox on page two and therefore doesn't insert
    > the AutoText on the first page. I keep thinking there *has* to be a way...!

    I did test that with the code snippet on your stripped down sample document and it worked (I changed blivit, too). Not sure what is going wrong with your real document...

  7. #7
    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: textbox in footer again (Word 2003 SP2)

    Can you try this inventory and see what results you get for a more complex document?
    Attached Files Attached Files

  8. #8
    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: textbox in footer again (Word 2003 SP2)

    Hi Hans,

    "Blivit" was defined in the original code. I edited it to be "ftrType" so it would make a little more sense - and of course I missed one.
    Option Explicit is at the top of all my modules for just this reason...

    Thanks, Kim

  9. #9
    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: textbox in footer again (Word 2003 SP2)

    Thank you for this great diagnostic code!

    I tried it on the few documents I currently have, which are all fairly simply footer-wise. Still, my results seem to indicate that I can't get there from here.

    On the simple document I originally sent, here's what I found - and I'm just looking at the first section in all my observations - when there's a different first page, VBA finds the same textbox as a Primary and as a First -- even when there is no textbox on the first page. When there is, VBA finds each textbox twice.

    When Different first page is unchecked, VBA finds the textbox only as a Primary.

    On a more complex document, with many shapes in both the header and the footer, with different first page checked and a textbox inserted on the first page, your macro found 12 Primary and 12 First page shapes. Most of those shapes were inserted into the header. If I deleted the box on the first page, it found 11 Primary and First page shapes. It looks like all the Shapes are shared by the HeaderFooter object and are not divied up by Header or Footer.

    You're right that the ShapeRange count changed - if there is a Different first page, the ShapeRange count was 1 for Primary and 1 for First. If there wasn't a different first page, the ShapeRange count for Primary was 2 and didn't exist for First. I know there are some conclusions that can be reached from all this data, but I still can't determine if my First page footer has a textbox in it or not - at least programatically.

    I will take Hans' advice and read up on Andrew's watermark code. I can't use inline shapes as I need to be able to move these guys out of the way of any other text in the footer. I will also see what Frames can give me, but if I can't decipher it today, I'll give up and go back to rolling through the document, entering each footer, and adjusting there.

    I sure am learning tons more about Headers and Footers and Shapes, though. <img src=/S/drop.gif border=0 alt=drop width=23 height=23>

    Thanks for everything,
    Kim

  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: textbox in footer again (Word 2003 SP2)

    I'm not sure why we get different results for the document you posted. I tried in both Word 2002 (office) and Word 2003 (home) and get the same results: footer.Shapes is useless, but footer.Range.ShapeRange sees them correctly.
    Attached Images Attached Images

  11. #11
    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: textbox in footer again (Word 2003 SP2)

    Your graphic does show what I get when reporting on the document I posted. I should have posted a screen shot as you did. I'm sorry if my narrative didn't say this.

    If there *is* a textbox on the First Page footer, my entries turn into:

    Primary - True - False - 2 - 1 - 0
    First - True - False - 2 - 1 - 0

    So it does look as if you can look to the ShapeRange to determine whether the box is displayed on the first page. But I still need to be able to identify a specific textbox. Or am I missing something? It wouldn't be the first time.

    But because my poor brain can't take any more of this, I've taken another tack -- to remove *all* existing instances of the textbox named "DocID" and then add them back to every page with this code:
    <pre>Sub AddDocID()
    Dim sec As Section
    Dim shp As Shape
    Dim rng As Range
    Dim ftr As HeaderFooter

    With ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary)
    If .Shapes.Count > 0 Then
    For Each shp In .Shapes
    If shp.Name = "DocID" Then shp.Delete
    Next shp
    End If
    End With

    Application.ScreenUpdating = False
    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close
    ActiveWindow.ActivePane.View.Type = wdPageView
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    'loop thru the footers in each section
    For Each sec In ActiveDocument.Sections
    For Each ftr In sec.Footers
    If ftr.Exists and Not ftr.LinkToPrevious Then
    Set rng = ftr.Range
    rng.Start = rng.End
    ActiveDocument.AttachedTemplate.AutoTextEntries("D ocID").Insert _
    where:=rng, RichText:=True
    End If
    Next ftr
    Next sec

    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Application.ScreenUpdating = True
    End Sub
    </pre>


    This puts the textbox on every page without disturbing anything already in the footers (thanks to previous help from Andrew). It's worked well so far with my limited testing. If someone somehow loses a footer, they can just run this to repair their document id.

    I'm exhausted. Thanks so much for all your help!
    Kim

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

    Re: textbox in footer again (Word 2003 SP2)

    That looks as though it should work, but you could make it more efficient by removing all the following lines

    <code>If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close
    ActiveWindow.ActivePane.View.Type = wdPageView
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    ...
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument</code>

    Since you are using ranges to add and delete content, you don't need to change the view or the selection.

    StuartR

  13. #13
    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: textbox in footer again (Word 2003 SP2)

    You're right. It works just fine without that - especially since it didn't need it.

    Thanks Stuart
    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
  •