Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced delete macro for Word 2

Status
Not open for further replies.

Bruce18W

Technical User
May 9, 2011
27
US
Hi,

I'm looking for a macro that will delete (or cut/copy) text from the cursor location to the nearest match of a pattern that the macro requests. For example, if the cursor is just after the "r" in "for" in the previous sentence, and I entered "cu" as the pattern, the macro should change:

I'm looking for a macro that will delete (or cut/copy) text from the cursor location to the nearest match of a pattern that the macro requests. To:
I'm looking for cursor location to the nearest match of a pattern that the macro requests.

Has anyone seen or written anything like this? It would great to have and tie to a keyboard shortcut.

Thanks, Bruce
 
Paul,

I tried the macro and it works when there is a pattern match. When there is no pattern match, it seems to get stuck in the while loop. I suppose one could add a counter in the loop and exit it after a certain number of attempts. Do you know of an easier way to abort the process when there is no pattern to match?

Thanks, Bruce
 
Simpler still, with error checking:
Code:
Sub KillContent()
Dim Str As String, Rng As Range
Str = InputBox("Text to find", "Content Killer")
With Selection
  .Collapse wdCollapseEnd
  Set Rng = ActiveDocument.Range(.Start, ActiveDocument.StoryRanges(.StoryType).End)
  If InStr(Rng.Text, Str) > 0 Then
    .End = .End + InStr(Rng.Text, Str)
    .Text = vbNullString
  Else
    MsgBox "No match found.", vbExclamation
  End If
End With
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top