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
 
hi,

What code do you have so far and where are you stuck. This is not a "conjure me up some code" forum.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Sorry if my request seemed presumptuous. This is what I have so far.

Sub DeleteNWords()

Dim WordsToDelete As String

WordsToDelete = InputBox("How many words to cut?")
Selection.MoveRight Unit:=wdWord, Count:=Val(WordsToDelete), Extend:=wdExtend
Selection.Cut

End Sub

So the key is getting input from a user (trivial), using this as a search string (less trivial), and extending the selection to a location just prior the first match.
 
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:
Please explain what "the macro should change" means???

How can the cursor be in "the previous sentence"???

Maybe 1) an actual example of text and 2) what expected results.

Is this a comprehensive list of business rules that describe any and all instances you hope to include?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip,

I thought I included an example of text in my original query. In any case. here's another example.

[ul]
[li]Original sentence: A boy walks down the road and falls.[/li]
[li]Cursor location: At the highlight, just after "boy"[/li]
[li]Search string: fa[/li]
[li]Text to be deleted: walks down the road and[/li]
[li]Final sentence: A boy falls.[/li]
[/ul]

This is the basic functionality I'm looking for. Wildcards would be nice, but I'd be more than satisfied with the basic functionality.

Thanks, Bruce
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try something along the lines of:
Code:
Sub KillContent()
Dim Shp As Shape
With Selection
  .MoveEndUntil Cset:=InputBox("Text to find", "Content Killer")
  .Text = vbNullString
End With
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
PS: delete 'Dim Shp As Shape' (left over from something else I was working on.

Cheers
Paul Edstein
[MS MVP - Word]
 
Paul,

thanks for the suggestion. It works, but it finds only a character, not a pattern.

[ul]
[li]Original text: Systems Thinking provides a disciplined way[/li]
[li]String I entered: di[/li]
[li]Cursor location: beginning of text[/li]
[li]Desired result: Systems Thinking provides a way[/li]
[li]Actual result: inking provides a way[/li]
[li]Problem: the "i" in the string I entered matched the i in "Thinking"[/li]
[/ul]Any suggestions regarding how to match a pattern and not just a character?

Thanks, Bruce

 
Bruce said:
Original text: Systems Thinking provides a disciplined way
String I entered: di
Cursor location: beginning of text
Desired result: Systems Thinking provides a way
If I compare that to this:
Bruce said:
Original sentence: A boy walks down the road and falls.
Cursor location: At the highlight, just after "boy"
Search string: fa
Text to be deleted: walks down the road and
Final sentence: A boy falls.

Then one of the two is a wrong description. I would assume the second to be wrong.
Please clarify.

To give you a further starting point, consider this:
Code:
Dim StartRan as Long, EndRan as Long

StartRan=Selection.End

This will store your current cursor position. If now you perform a simple "Selection.Find" with your pattern input, you can use something like this:
Code:
EndRan=Selection.Start
to get the second range position.

Then you can use
Code:
ActiveDocument.Range(StartRan, EndRan).Delete
to delete the desired portion.

That OK?

Cheers,
MakeItSo
[/code]


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Argh! I meant the "first to be wrong" (the one marked red).
[blush]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
thanks for the code tips. I'll work on it. As for my example, I'll try again:

[ul]
[li]Original text: Systems Thinking provides a disciplined way[/li]
[li]Cursor location: beginning of line[/li]
[li]String I entered: di[/li]
[li]What should be deleted: "Systems Thinking provides a"[/li]
[li]Desired result: way[/li]
[li]What is deleted: "Systems Th"[/li]
[li]Actual result: inking provides a disciplined way[/li]
[/ul]

Problem: the "i" in the string I entered matched the i in "Thinking"

Your code tips should help. I'll post a solution when I have it.

Bruce
 
Hi Bruce,

here's something for you to play with:
Code:
Sub DelPart()
Dim StartRan As Long, EndRan As Long
Dim patrn As String
StartRan = Selection.End
patrn = InputBox("Please enter character pattern:", "Delete portion")

With Selection
    .Collapse wdCollapseEnd
    With .Find
        .ClearFormatting
        .MatchWildcards = False
        .Format = False
        .Text = patrn
        .Execute
    End With
    If .Find.Found Then
        EndRan = .Words(1).End
        ActiveDocument.Range(StartRan, EndRan).Delete
        .Collapse wdCollapseEnd
    End If
End With

End Sub

P.S: In your first example, "A boy walks down the road and falls." you have to search for pattern "an" to receive "A boy falls", with "fa" you wil receive "A boy" only.
;-)

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thanks, MakeItSo. I worked on my own code (before seeing yours) and came up with this:

Sub KillContent()

Dim StartRan As Long, EndRan As Long, TargetStr As String

TargetStr = InputBox("Text to find", "Content Killer")
StartRan = Selection.End

With Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = False
.MatchCase = True
.Wrap = wdFindContinue
.Execute FindText:=TargetStr
End With

EndRan = Selection.Start

ActiveDocument.Range(StartRan, EndRan).Delete
Selection.Collapse Direction:=wdCollapseStart


End Sub

I also made a shortcut to ctrl-g for easy access. I'll look at your code to see if I can improve mine.

thanks again, Bruce (PS I think this will be quite handy for editting. Also, how do you put the "code" block around code in your reply above?)
 
[ignore]
Code:
this is my code
[/ignore]

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
...or select your text and use the code icon above.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
thanks, Skip. I tried the code icon, but it didn't change the text. Thanks for the telling me about

Code:
code

[bigsmile]Bruce
 
Well that looks like a good start.
You might also want to check for spaces to make sure the end result is not concatenated "likethis".


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Simpler:
Code:
Sub KillContent()
Dim Str As String
Str = InputBox("Text to find", "Content Killer")
With Selection
  .Collapse wdCollapseEnd
  While InStr(.Text, Str) = 0
    .MoveEndUntil cset:=Left(Str, 1)
    .End = .End + Len(Str)
  Wend
  .End = .End - Len(Str)
  .Text = vbNullString
End With
End Sub
You'll note that I'm not using '.Delete'. Using '.Text = vbNullString' instead avoids the space that '.Delete' sometimes inserts in place of the deleted content.

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks, Paul. I'll give it a try. Nice technique. Bruce
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top