Word 2003, macro to find txt, delete specific part of line, contin

T

Tailings

One more thing I am having problems with - I need to find some specific text
- space, period, space, space, PAC. This is the only common item to all the
lines that need to be changed, and always occupies the same position. Once
this is found, I need to go to the beginning of the line (all lines are
actually paragraphs) and delete up to and including the space before PAC.

I need the macro to continue thru the entire document.

I recorded the following:

Sub RemoveBeginningOfLine

Selection.Find.ClearFormatting
With Selection.Find
.Text = ". PAC"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
How do I make it continue?
 
T

Tony Strazzeri

you create a loop from after the execute statement using the
Selection.Find.Found property to test if the find has succeded.

The following should work, but I advise stepping through to ensure it
works as I have not tested it.

Cheers
TonyS.

Sub RemoveBeginningOfLine()

Selection.Find.ClearFormatting
With Selection.Find
.Text = ". PAC"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Do While Selection.Find.Found
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

Selection.Find.Execute
Loop

End Sub
 
T

Tailings

Worked like a charm! I had found a workaround, but your solution is FAR more
elegant! Thanks SO much!
 
H

Helmut Weber

Hi,

so, if a paragraph starts with " . PAC"
you want to remove " . " from that paragraphs start?

Note, that you talk about
- space, period, space, space, PAC
while searching for period, space, space, PAC!

The information, that the search string is always
at the same position isn't very helpful,
as one would have to find out the position first.
It could help to speed up the macro, in theory,
as then no searching would be necessary at all.

Also:
This is the only common item to all the
lines that need to be changed,

tells, that all lines to be processed, contain " . PAC",
not that all lines containing " . PAC"
have to be processed. ;-)

However, I don't think you need more than this,
which does not check whether " . PAC" is at the
paragraph's start.

With ActiveDocument.Range.Find
.Text = " . PAC" ' leading space!
.Replacement.Text = "PAC"
.Execute Replace:=wdReplaceAll
End With

Still a bit a stab in the dark.

You may use the selection object, too,
search for "^p . PAC", and replace it with "^pPAC".

But as this would't work for the first paragraph,
I don't like it.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tailings

Sorry I wasn't very clear - or precise! I have been battling with this
project for several days and even dreamed I had the solution. How annoying to
wake up and find it still didn't work.

I am cutting and pasting text from a very primitive database and putting it
in what is supposed to be a relatively sophisticated-looking report. Right
now we go through and delete all the unneeded info manually, which take a
very long time. The macro work is speeding it up.

Thanks to all for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top