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

    What's going on with my footer? (2003 (SP2))

    A while back, I got help here regarding footers, cycling through all of them and putting a text box in each if there wasn't one. The text box has two fields that display information about the document name and author, which info it gets from DOCS.

    It works perfectly *except* when you run it for the first time after you've opened Word. I've attached the code and highlighted the line of code that fails. The first time this code is run in a Word session, that line causes the macro to bypass the code to insert the textbox. However, it *always* works every time after that, until you exit and re-load Word.

    I just don't get it. <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23> Can anyone explain?

    Thanks!! Kim
    Attached Files Attached Files

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

    Re: What's going on with my footer? (2003 (SP2))

    <P ID="edit" class=small>(Edited by Andrew Lockton on 27-Mar-07 15:47. Rereading your question)</P>I would have guessed that the LinkToPrevious will fail in the first section of the document since there is no previous section there. Is that the component that is failing?
    When I tested the code, however it worked happily and sprang no errors on me. Perhaps you would need to add a test to check the section number before running the LinkToPrevious

    What is the exact error message you are getting with this line?
    ===================
    Added: If you are not getting an error then you can ignore the above response.

    You need to isolate which condition is not being met on the first run of the code. Try stepping through it to check the values of the LinkToPrevious and the Exists on the first and subsequent runs. Perhaps you could add a couple of lines to record these values in the immediate window.
    <pre>Sub AddTextbox()
    Dim sect As Section
    Dim oShape As Shape
    Dim rng As Range
    Dim hf As HeaderFooter
    Dim I as integer
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary)
    If .Shapes.Count > 0 Then
    For Each oShape In .Shapes
    If oShape.name = "DocID" Then oShape.Delete
    Next oShape
    End If
    End With
    Application.ScreenUpdating = False
    For Each sect In ActiveDocument.Sections
    For Each hf In sect.Footers
    I = I + 1
    Debug.Print I &
    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: What's going on with my footer? (2003 (SP2))

    Thanks Andrew,

    No error message, it just doesn't insert the text box. It evaluates the If statement and decides it isn't right. But only the first time I run it in a new session of Word. When you tested the code, did you try it immediately after opening Word?

    It works for me every other time I run it, in any document. Every other time, in every section, including the first. I don't want it to skip the first section and I don't want it to insert multiple textboxes in any one footer... I just need it to get past the first time it's run without evaluating negatively and skipping the textbox insertion.

    I thought Word might be having trouble recognizing the footer (it just woke up, after all). If I run it on a document that has page numbers in the footer, it won't work. If I simply run another macro, it won't work. But if I run a macro that accesses the footer before I run this one, then this one will work. I've attached this macro -- it, too, uses an If statement that evaluates LinkToPrevious, but it works. I'll try reversing the .Exists and .LinkToPrevious to see if that makes a difference but...

    I remain, <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23> (Kim)
    Attached Files Attached Files

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

    Re: What's going on with my footer? (2003 (SP2))

    What happens if you try this variation?

    With ftr
    If .Exists Then
    If Not .LinkToPrevious Then
    ...
    End If
    End If
    End With

  5. #5
    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: What's going on with my footer? (2003 (SP2))

    I'll try it. Thanks, Hans.

  6. #6
    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: What's going on with my footer? (2003 (SP2))

    OK...

    Before trying Hans' solution, I simply reversed the two properties in the line of code that was the problem:
    <pre>If hf.Exists And Not hf.LinkToPrevious Then</pre>

    And now it works all the time. <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> Not sure why this doesn't work when Word first wakes up, but the order seems to matter.

    Thank you both so much!

    Kim

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

    Re: What's going on with my footer? (2003 (SP2))

    Apparently VBA is smart enough to stop checking if the first condition is False.
    With the original order, you'd run into a problem if the header or footer doesn't exist - the code first checks LinkToPrevious, but of course this makes no sense if the header/footer doesn't exist.

Posting Permissions

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