PDA

View Full Version : Solved: Delete characters in a row until paragraph mark is found



Jiltdijk
10-30-2007, 07:51 AM
Dear forum,

In a Word document, in a line looking like:

- Speaking skills: The household [TV TB p98:3]

I want a macro to delete the part in brackets. In the code below it's the loop in the last three lines that doesn't work. The macro just keeps erasing until I hit the break key.



Sub EraseRemark()

Selection.Find.ClearFormatting
With Selection.Find
.Text = "["
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute

Do While Selection.Text <> "^p"
Selection.Delete
Loop
End Sub


Thank you for your help.
Jilt

OTWarrior
10-30-2007, 08:14 AM
instead of
Do While Selection.Text <> "^p"
try
Do While Selection.Text <> vbcrlf

or even try:

Do While Selection.Text <> "]"

then delete the "]" afterwards


hope that helps

fumei
10-30-2007, 12:51 PM
Better to not use selection at all.

IF your "line" is in fact a paragraph - it terminates with a paragraph mark - you can use this:Sub EraseRemark()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(Findtext:="[", Forward:=True) = True
r.MoveEndUntil Cset:=vbCrLf
r.Delete
Loop
End With
Set r = Nothing
End Sub

fumei
10-30-2007, 12:57 PM
If your "[TV TB p98:3]" does NOT terminate with a paragraph mark, alternatively you can use:Sub EraseRemark()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="[", Forward:=True) = True
With r
.MoveEndUntil Cset:="]"
.MoveEnd Unit:=wdCharacter, Count:=1
.Delete
End With
Loop
End With
Set r = Nothing
End SubThis finds the "[", extends until it finds "]", extends one more to include the "]", then deletes.

Note: using Cset extends the range up to the given Cset parameter. Which is why you need to extend one more character to include it.

Generally speaking, it is much better to use Range, rather than Selection. Selection always interacts with the user interface (the screen display). Range does not, so no GUI resources are used.

Jiltdijk
10-30-2007, 02:02 PM
IF your "line" is in fact a paragraph - it terminates with a paragraph mark

Yes, it's a paragraph. Thank you, you're code does the trick.

Jilt

TonyJollans
10-31-2007, 02:39 AM
Why not use the Find & Replace to do the deletion and save yourself the trouble of trying to do it?

With ActiveDocument.Range.Find
.Text = "\[*\]^13"
.Replacement.Text = "[]^p"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

fumei
10-31-2007, 12:14 PM
That would work if it is, in fact, terminated by a paragraph mark.

Although, interesting how we interpret things.

Here is the text from the OP.

"In a Word document, in a line looking like:

- Speaking skills: The household [TV TB p98:3]

I want a macro to delete the part in brackets. "

I interpreted that as removing the parts in brackets, including the brackets. My code went for that.

Tony's code does not. It removes "the part in brackets", and keeps the brackets.

Just a curious observation on how we view things through our own sets of assumptions.

In this case, Tony is correct as a literal response to the OP. It did NOT state the removal of the brackets.

TonyJollans
10-31-2007, 12:22 PM
Thank you for that dissection, Gerry :)

The original post did imply that the brackets were to be removed - actually it implied that everything up to the paragraph mark be deleted.

My point was that whatever was actually wanted, it could be achieved with F&R alone, without resorting to scanning characters afterwards.

fumei
10-31-2007, 12:35 PM
Very true, and I was not trying to be critical. Your code is better. It comes from your deeper understanding and use of wildcards in F & R. Not my strongest suit.

Jiltdijk
10-31-2007, 03:45 PM
Although, interesting how we interpret things.

That's true! It also shows how difficult it is (for me at least) to write in a precise manner what you mean.

fumei
11-06-2007, 12:18 AM
That is why some of us - with more or less degrees of diplomacy - try to relentlessly encourage people to write precisely and accurately, with as few assumptions as possible.

It is not just you, it is a a sign of the time. There is a general lessening of accurate, precise language. Unfortunately, VBA (and other computer languages) DO require accurate, precise language.

Until such a time as direct mind to system connections exist, we will have to make do with words, syntax and context. This takes work.