Deleting lines with VB Macro

D

dado_maker

After finding a line that contains a word, how do I delete the whole
line with VB code? The line can end with a Paragrapgh marker or a line
break.

Thanks,
dado_maker
 
G

Greg Maxey

dado_maker,

This can most likely be improved, but seems to work. This macro looks for
the string ????? in a paragraph object. If found, then is looks for a line
break. If the line break "is not" found then the whole paragraph
(supposedly a single line paragraph in your case) is deleted. If a line
break is found, then the the paragraph range is set to myRange, myRange is
then collasped then the end is moved to the line break + 1 (or then end of
the line including the break) and myRange is deleted.

Sub ScratchMacro()
Dim myRange As Range
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Paragraphs
If InStr(oPar.Range.Text, "?????") Then
If InStr(oPar.Range.Text, Chr(11)) Then
Set myRange = oPar.Range
myRange.Collapse
myRange.MoveEndUntil (Chr(11))
myRange.MoveEnd Unit:=wdCharacter, Count:=1
myRange.Delete
Else
oPar.Range.Delete
End If
End If
Next

End Sub
 
D

dado_maker

Greg,

Thanks! Is there a VBA for MS Word out there you would recomend?

Thanks,
dado_maker
 
G

Greg Maxey

dado_maker,

I haven't read any books on VBA. I don't know much and all that I do know
is from diving in to these newsgroups, asking lots of questions, and reading
the material at the MVP site.
 

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