Macro for Paragraph

J

Julie

Hi All! Is there a way that I can have a macro look for
a certain word in a report, but only when it is at the
start of a line and not in the middle of a name. For
example:

I need to delete the word "MED" out of a report. It is
on multiple lines, and the number of lines varies per
report. The word "MED" can also be found in certain
words like MEDical, interMEDiate, or arMED. How do I get
the macro to only go to the start of the line
(paragraph), grab the information I need to delete, and
then move to the start of the next line (not grabing
other words)? I have posted what I have below. Thanks
in advance and have a great weekend.

Julie

Selection.Find.ClearFormatting
With Selection.Find
.Text = "MED"
.Replacement.Text = _
"

"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do
Selection.Find.Execute
If Selection.Find.Found = True Then
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.MoveRight Unit:=wdCharacter,
Count:=18, Extend:=wdExtend
Selection.TypeBackspace
Else
Exit Do
End If
Loop
 
J

JGM

Hi Julie,

Try this:

Sub RemoveMed()

Dim myPara As Paragraph
Dim myDoc As Document

Set myDoc = ActiveDocument

For Each myPara In myDoc.Paragraphs
If LCase(myPara.Range.Words(1).Text) = "med " Then
myPara.Range.Words(1).Delete
Next myPara

End Sub

HTH
Cheers!
 
E

Ed

As I suggested yesterday to "Ann" who had a similar situation (and to whom I
gave similar code), you might try using .Text = "^pMED". If it's the
starting text of a paragraph, then it has a paragraph mark ("^p") in front
of it. Including that in your text to search for will give you only "MED"
which follows a paragraph break, meaning that "MED" is the starting text of
a new paragraph.

Ed
 
J

Julie

Hi Ed,

Thanks for your help on this problem. For the search
text, "^pMED" didn't work, but I did put "MED " and it
worked. Thanks again and have a great day!

Julie
 
J

Julie

Thanks for your help, but this didn't work for me. I
used the code that I already had and I used "MED " as the
search text instead of "MED". Thanks for your time!

Julie
 

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