• Removing Tabs Around Footnote Numbers

    Author
    Topic
    #474642

    I’ve been trying to write a simple macro for Word 2010 to delete a tab on either side of a footnote number. I have huge numbers of files that come in with these and they have to be deleted before I format them.

    Here is what I have so far. What I can’t figure out is how is to make it remove all of them in one swoop, instead of just the first one:

    Sub ReplaceTabsInNotes()

    Selection.GoTo What:=wdGoToFootnote, Which:=wdGoToFirst, Count:=1, Name:= _
    “”
    With Selection.Find
    On Error Resume Next
    .Text = vbTab + “^f”
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    While Selection = vbTab
    Selection.Delete
    Selection.Find.Execute
    Wend
    Selection.Find.Execute
    End With
    ActiveDocument.UndoClear

    Selection.GoTo What:=wdGoToFootnote, Which:=wdGoToFirst, Count:=1, Name:= _
    “”
    With Selection.Find
    On Error Resume Next
    .Text = “^f” = vbTab
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    While Selection = vbTab
    Selection.Delete
    Selection.Find.Execute
    Wend
    End With
    ActiveDocument.UndoClear

    End Sub

    Thanks in advance for any help you can give.

    Viewing 3 reply threads
    Author
    Replies
    • #1266143

      This can probably be done more elegantly using wildcards, but here’s a revised version of your code, that seems to work:

      Code:
      Sub DeleteFNTabs()
         With Selection.Find
            .ClearFormatting
            .Text = “^t^f”
            .Replacement.Text = “”
            .Forward = True
            .Wrap = wdFindContinue
            .Execute
            Do While .Found
               Selection.MoveLeft Unit:=wdCharacter, Count:=1
               Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
               Selection.Delete Unit:=wdCharacter, Count:=1
               Selection.MoveRight Unit:=wdCharacter, Count:=1
               .Execute
            Loop
         End With
         With Selection.Find
            .ClearFormatting
            .Text = “^f^t”
            .Replacement.Text = “”
            .Forward = True
            .Wrap = wdFindContinue
            .Execute
            Do While .Found
               Selection.MoveRight Unit:=wdCharacter, Count:=1
               Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
               Selection.Delete Unit:=wdCharacter, Count:=1
               .Execute
            Loop
         End With
      End Sub

      Gary

    • #1266144

      AFAIK you can’t use a wildcard Find for footnote markers. However, the code can be simplified:

      Code:
      Sub DeleteFNTabs()
         With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWildcards = False
            .Text = "^t^f"
            .Replacement.Text = ""
            .Execute
            Do While .Found
               Selection.Characters.First.Delete
               .Execute
            Loop
            .Text = "^f^t"
            .Execute
            Do While .Found
               Selection.Characters.Last.Delete
               .Execute
            Loop
         End With
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1266216

        Paul,

        That’s definitely more elegant, and it’s good to be reminded how useful .First and .Last can be, but I found I needed to add two lines of code to get it to run right:

        Code:
        Sub DeleteFNTabs2()
           With Selection.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Forward = True
              .Wrap = wdFindContinue
              .MatchWildcards = False
              .Text = “^t^f”
              .Replacement.Text = “”
              .Execute
              Do While .Found
                 Selection.Characters.First.Delete
                 .Execute
              Loop
              ‘Added line:
              .Wrap = wdFindContinue
              .Text = “^f^t”
              .Execute
              Do While .Found
                 Selection.Characters.Last.Delete
        ‘         ‘Added line:
                 Selection.MoveRight Unit:=wdCharacter, Count:=1
                 .Execute
              Loop
           End With
        End Sub

        Without the first added line, the second .Execute was not resulting in anything Found, so that whole part was being skipped. Just by trial and error, I found that adding back in the wdFindContinue, got the second .Execute to find something.

        Even with that in place, it then would only find the first instance of the “^f^t” string – stepping through it, it looks like “Selection.Characters.Last.Delete” leaves the first found character still selected, so the Find loop then goes back to search that character only, rather than moving forward through the remainder of the doc. Moving the cursor one to the right fixed that (I guess a .CollapseEnd would work too).

        Gary

    • #1266206

      Thanks to both of you! I think one of those two should work.

      I wasn’t aware that you could have it delete any character to the right or left like that.

      I learned something new.

    • #1269253

      Hi Gary,

      I’ve been away for a few weeks, hence the delay in replying.

      I’m surprised you needed to add in a second ‘.Wrap = wdFindContinue’, as that property was already set.

      As for the ‘Selection.MoveRight Unit:=wdCharacter, Count:=1’ line, I think a simple ‘.Collapse’ would be better so as to clobber any multiple tabs following the footnote marker.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1269719

        Paul,

        Glad you’re back! (I was just away, for a few days, myself.)

        I’ve just gone back and retested, and now I can’t reproduce the problem that forced me to add in the second ‘.Wrap = wdFindContinue’ (didn’t make sense to have to do so, but seemed to be the only way to get it to work, at that time). Just one of those quirky things…

        And yes, a simple .Collapse (which defaults to the start of the selection) seems to do the trick.

        Gary

    Viewing 3 reply threads
    Reply To: Removing Tabs Around Footnote Numbers

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: