Macro for deleting lines which begin with the same words

M

Marcel

Hello,
how can I delete all lines in my word document which beginn for example with
2 NICK?

I am new in vba so please for beginners, thanks :)

Marcel
 
D

Doug Robbins - Word MVP on news.microsoft.com

Are these lines or paragraphs? If you click on the Show/Hide button (¶) is
there a ¶ at the end of each line.

If they are paragraphs, use:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^p2 NICK", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindContinue, MatchCase:=True) = True
Selection.Paragraphs(2).Range.Delete
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Greg Maxey

Hello,
how can I delete all lines in my word document which beginn for example with
2 NICK?

I am new in vba so please for beginners, thanks :)

Marcel

If they are not independent paragraphs and you are using Word2007 (I
don't think it is available in earlier versions) you can use the
Panes>Pages>Rectangles>Lines objects likes this:

Sub ScratchMacro()
Dim objPage As Page
Dim i As Long
Dim j As Long
Dim MyRect As Rectangle
Dim oRng As Range
For i = 1 To ActiveDocument.ActiveWindow.Panes(1).Pages.Count
Set objPage = ActiveDocument.ActiveWindow.Panes(1).Pages(i)
Set MyRect = objPage.Rectangles(1)
For j = 1 To MyRect.Lines.Count
Set oRng = MyRect.Lines(j).Range
With oRng.Find
.Text = "2 NICK"
If .Execute Then
MyRect.Lines(j).Range.Delete
End If
End With
Next j
Next i
End Sub
 
M

macropod

Hi Marcel,

If your lines are terminated by manual line breaks or paragraph breaks, you don't actually need a macro for this - you can use
Word's Find/Replace function.

For example, to replace all lines terminated by manual line breaks, make the Find text:
^11<2 NICK>*^11
and the Replace Text:
^l
Similarly, to replace all lines terminated by paragraph breaks, make the Find text:
^13<2 NICK>*^13
and the Replace Text:
^p
In both cases, unless the found line is the 1st line in the document, it will be deleted - just the same as with Doug's macro. A
macro equivalent would be:
Sub ScratchMacro()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "^11<2 NICK>*^11"
.Replacement.ClearFormatting
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
I suspect this would be slightly faster than Doug's macro too ;)
 
F

fumei via OfficeKB.com

Or, if they are indeed paragraphs...

Sub Dump2Nick()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
If Left(oPara.Range.Text, 6) = "2 Nick" Then
oPara.Range.Delete
End If
Next
End Sub
 

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