Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Richmond, Virginia, USA
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Writing an If...Then statement (Word 2000 SR-1)

    I have lots of problems writing macros, and I'd love a little help. I'm trying to write an if, then statement in the middle of an existing macro. I want a popup box to appear if the document the macro is being run on has any headers, footers or footnotes in it. I want the popup to allow me to choose to either continue running the macro, or to stop the macro and go back to the document. If there are no headers, footers or footnotes, I just want the macro to run as normal.

    I'm assuming that this is possible, but I could be quite mistaken.

    Thanks in advance for any help,
    Becky

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

    Re: Writing an If...Then statement (Word 2000 SR-1)

    Becky,

    The following example should give you something to work with:

    <pre>Public Sub TestForHeaderFooterFootnotes()
    Dim nResp1 As Long
    Dim nResp2 As Long

    If ActiveDocument.Footnotes.Count > 0 Then
    nResp1 = MsgBox("Document contains footnotes - " & _
    "Click OK to continue; Cancel to end processing", _
    Buttons:=vbOKCancel)
    If nResp1 = 2 Then Exit Sub
    End If

    '(The StoryRanges Count is 1 if there is no header/footer content)
    If ActiveDocument.StoryRanges.Count > 1 Then
    nResp2 = MsgBox(Prompt:="There is header/footer content - " & _
    "Click OK to continue; Cancel to end processing", _
    Buttons:=vbOKCancel)
    If nResp2 = 2 Then Exit Sub
    End If

    'rest of your code here - only runs if you get past the above tests
    'MsgBox "Test - the Sub wasn't ended by the user"
    End Sub
    </pre>

    Gary

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Writing an If...Then statement (Word 2000 SR-1)

    Gary,

    StoryRanges.Count can be greater than 1 if there are Comments, EndNotes or TextBoxes.

    If you explicitly check for the presence of the various footnote, header and footer StoryRange types, the results will be more reliable. Becky, give this a whirl:

    <pre>Sub YourMacro()

    Dim S As Section, NoneOfAbove As Boolean, MsgText As String, _
    MsgType As Integer, MsgTitle As String

    NoneOfAbove = True

    For Each S In ActiveDocument.Sections
    NoneOfAbove = NoneOfAbove And _
    S.Headers(wdHeaderFooterEvenPages).Range.Text = vbCr
    NoneOfAbove = NoneOfAbove And _
    S.Headers(wdHeaderFooterFirstPage).Range.Text = vbCr
    NoneOfAbove = NoneOfAbove And _
    S.Headers(wdHeaderFooterPrimary).Range.Text = vbCr
    NoneOfAbove = NoneOfAbove And _
    S.Footers(wdHeaderFooterEvenPages).Range.Text = vbCr
    NoneOfAbove = NoneOfAbove And _
    S.Footers(wdHeaderFooterFirstPage).Range.Text = vbCr
    NoneOfAbove = NoneOfAbove And _
    S.Footers(wdHeaderFooterPrimary).Range.Text = vbCr
    NoneOfAbove = NoneOfAbove And (ActiveDocument.Footnotes.Count = 0)

    Next S

    If NoneOfAbove Then
    ' There are no headers, footers or footnotes, keep going after End If

    Else
    MsgText = "This document contains at least one header," & vbCrLf & _
    "footer or footnote. Do you wish to continue?"
    MsgType = vbYesNo + vbQuestion + vbDefaultButton2
    MsgTitle = "Maybe the macro name here..."
    If MsgBox(MsgText, MsgType, MsgTitle) = vbNo Then Exit Sub

    End If

    ' The existing macro goes between here...

    ' ... and here.

    End Sub

    </pre>


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

    Re: Writing an If...Then statement (Word 2000 SR-1)

    Jim,

    Thanks - my look at this was only superficial so it's good to see an in-depth approach.

    BTW that's a very interesting logical structure - I follow what you're doing, but never would have thought of that!

    One side-effect of this though is that it inserts that visible paragraph return into every header and footer - don't know if that matters really:
    Based on how your code works, it seems every headerfooter in a new document contains that vbCr, whether it's visible or not - kind of odd.

    Gary

  5. #5
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Richmond, Virginia, USA
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Writing an If...Then statement (Thanks)

    <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30> Absolutely fabulous!!!!!! Thanks so much to both of you for your help. The macro ran perfectly right from the start.

    Thanks again
    Becky <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  6. #6
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Writing an If...Then statement (Word 2000 SR-1)

    Hi Gary,

    Yeah, you're right about the code "inserting" a paragraph mark. I noticed the same thing because, my default header format has a bottom border (to separate it from the main body), and after running the macro, the line shows up, even in print preview. But if I choose View (menu), Header and Footer, then clicked Close, the bottom border disappears, even though the paragraph mark is still in there. It's weird.

Posting Permissions

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