none
can't batch add [] for footnote number via vba RRS feed

  • Question

  • hello

    I use the Find and Replace to batch add "[ ]" for footnote numbers and it works well.  so I record the macro for the process ,the macro code are below.

    Sub Macro6()
    '
    ' Macro6 Macro
    '
    '
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^f"
            .Replacement.Text = "[^&]"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

    But when I run the macro ,  only the footnote numbers in the text are added the “ [ ]”.  numbers under the footnote separator are not added the “ [ ]”.  

    I guess that the "Selection" commandin the macro code can't select the footnote annotation. Right? And how to select it and batch add [] for footnote number via vba?

    Any help will be greatly appreciated

    • Edited by JO1221 Monday, March 20, 2017 8:36 AM
    Monday, March 20, 2017 8:34 AM

Answers

  • That's because macros only operate on the main story range unless you code them to do otherwise. Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument
      Call UpdateRange(.Range)
      For Each Rng In .StoryRanges
        If Rng.StoryType = wdFootnotesStory Then
          Call UpdateRange(Rng): Exit For
        End If
      Next
    End With
    Application.ScreenUpdating = True
    End Sub

    Sub UpdateRange(Rng As Range)
    With Rng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "^f"
      .Replacement.Text = "[^&]"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .Execute Replace:=wdReplaceAll
    End With
    End Sub

    Note: the For ... Next loop guards against errors that would otherwise occur if you ran the code in a document with no footnotes.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, March 20, 2017 11:56 AM

All replies

  • That's because macros only operate on the main story range unless you code them to do otherwise. Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument
      Call UpdateRange(.Range)
      For Each Rng In .StoryRanges
        If Rng.StoryType = wdFootnotesStory Then
          Call UpdateRange(Rng): Exit For
        End If
      Next
    End With
    Application.ScreenUpdating = True
    End Sub

    Sub UpdateRange(Rng As Range)
    With Rng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "^f"
      .Replacement.Text = "[^&]"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .Execute Replace:=wdReplaceAll
    End With
    End Sub

    Note: the For ... Next loop guards against errors that would otherwise occur if you ran the code in a document with no footnotes.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, March 20, 2017 11:56 AM
  • Hi,Paul

    Thank you soooo much . That's exactly what I want! Thanks again!

    Tuesday, March 21, 2017 4:08 AM