PDA

View Full Version : Delete variable amount of text



AyeSee
01-24-2011, 01:04 PM
Heyhey,

I've been trying to have code delete a variable amount of my document (which can include pictures and tables). All of these need to be deleted depending on one condition. The problem is I have trouble selecting the lower and upper borders of what I need to delete.

I have set key words so that I can find the starting and end points, but I am having trouble selection everything in between both.

This is an illlustration of what I am trying to do (initial state):


----------------------------------
Bla bla bla bla bla bla bla bla.

keyFixedRate

Bla bla bla bla bla. Bla bla bla bla:

Bla bla bla bla bla.

Bla bla ble ble ble bla bla bla.

keyFixedRate

Bla bla bla bla bla bla bla bla.

----------------------------------

So, everything in between both keywords I have identified as "keyFixedRate" need to be deleted (inclusively). The final result should be like this:

----------------------------------
Bla bla bla bla bla bla bla bla.

Bla bla bla bla bla bla bla bla.

----------------------------------

I have found somethign similar that seems to do what I am looking for, but I can't seem to modify the code appropriately.
Dim myRange as Range
Set myRange = Selection.Range
myRange.End = ActiveDocument.Range.End
myRange.End = myRange.Start + Instr(myRange, "#")
myRange.Delete


Many thanks in advance...
Alex

fumei
01-24-2011, 01:30 PM
Sub BetweenTheSheets()
Dim myKeyString As String
Dim r As Range
Dim r2 As Range
myKeyString = "keyFixedRate"
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:=myKeyString, Forward:=True) = True
Set r2 = r.Duplicate
r.Find.Execute
With r2
.End = r.End
.MoveEnd Unit:=wdCharacter, Count:=1
.Delete
End With
Set r2 = Nothing
Loop
End With
Set r = Nothing
End Sub


Caveats: are the paragraphs true paragraphs? Not manual lines breaks? Each terminates with a paragraph mark?

Depending on whether you are using paragraphs properly it should work correctly. If you are using manual line breaks, or extra paragraphs, it may come out slightly off. These can be fixed.

Click "Between The Sheets" on top toolbar for demo.

fumei
01-24-2011, 01:32 PM
And to make it a wee bit shorter...

With r2
.End = r.End + 1
.Delete
End With

AyeSee
01-24-2011, 01:58 PM
Thanks again Gerry,

That does the job perfecly!

cc_brewster
01-28-2011, 02:25 PM
I suspect Gerry's code is the right way to accomplish this, but a similar thing can be done with bookmarks. This is easy to record from Word. I'm not sure how much you're doing manually versus in the code, but when you're at either the start or end point, you can add a bookmark there. Then at the starting point, use Selection.extend in the code (which is activated by F8 in Word), then use "go to bookmark" (Ctrl-G in Word) to go to the ending point bookmark. The whole range is then selected. If you don't want the bookmarks to stay in the doc, they can be removed using code.

fumei
02-01-2011, 10:48 AM
Oh for sure. If you have the chunk bookmarked it becomes easy. Heck you do not even need to go anywhere with the Selection. No need for a starting or ending point at all.

ActiveDocument.Bookmarks("ThisOne").Range.Delete

Done.