Find and Replace Using A Macro

J

Jerry

Sirs,

I'm very new at this VBA stuff so I hope someone can help me. I have about
2,850 occurrences of various words/lines in my Word document that I need to
delete. I could use the Find/Replace, but I'm looking for a simple macro
that would find a word, delete it, and then move to the next occurrence and
do the same thing. I need to macro to stop when it gets to the end of the
document. If I can write a macro, I can simply edit it with different
words. I've checked the Internet for samples, but none of them seem to
work. Can someone help me with this.

Example:

I need to find "document" and delete it.

TIA,
Jerry
 
J

Jerry

Sirs,

I think I need to clarify what I wrote earlier. What I'm trying to do is
find a string of letter at the beginning of a line, select to the end of
the line, and delete the line.

I have this so far: What I need to do is loop this until the macro no
longer finds the string (i.e. "CCF DATE ")

Sub Delete_line()
'
' Delete_line Macro
' alt a
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "CCF DATE "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
 
H

Helmut Weber

Hi Jerry,

Sub test6666()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
ResetSearch
With rDcm.Find
.Text = "document"
.Replacement.Text = ""
.MatchWholeWord = true
' otherwise "documents" will leave "s" behind
.Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

There may be double spaces left over,
if there were no double spaces before.
Otherwise there my be triple, quadruple etc. spaces.
Just use the above to search for " " and replace with " ",
until nothing is left.

There are more advanced methods, of course,
and that isn't the whole story, by far.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Helmut Weber

Hi Jerry,
now it seems you want to delete all paragraphs,
which start with "CCF DATE ".
Note the trailing space!

Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If Left(oPrg.Range.Text, 9) = "CCF DATE " Then
oPrg.Range.Delete
End If
Next

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
D

David Sisson

Sub Delete_line()
'
' Delete_line Macro
' alt a
'
Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "CCF DATE "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

'End of line, not End of paragraph
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Delete
Loop While Selection.Find.Found
End Sub
 
J

Jerry

Mr. Sisson,

This solution works just fine. Thank you for providing it. I do have
one small problem though. When I run this macro, it deletes the next
line following the last "find" text string. Is there a way to solve this
issue?

/r
Jerry
 
D

David Sisson

Sub Delete_line()
'
' Delete_line Macro
' alt a
'
Do
Selection.Find.ClearFormatting
With Selection.Find
.Text = "CCF DATE "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

If Selection.Find.Found Then
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Delete ' Unit:=wdCharacter, Count:=1
End If
Loop While Selection.Find.Found
End Sub
 
J

Jerry

Mr. Sisson,

This works just great. Thanks a bunch for your help. I really
appreciate it.

/r
Jerry
 

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