Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Removing Tabs Around Footnote Numbers

    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.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    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

  3. The Following User Says Thank You to Gary Frieder For This Useful Post:

    Yuwipi Woman (2011-02-05)

  4. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    Yuwipi Woman (2011-02-05)

  6. #4
    Star Lounger
    Join Date
    Nov 2003
    Posts
    61
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  7. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    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

  8. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  9. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    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

Tags for this Thread

Posting Permissions

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