Discussion Locked

Macro in MS word for deleting lines

0 helpful
12 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I'm looking for a macro that will delete lines that contain certian a word in a 10,000 page document I have. I'm not quite sure how to go about this in word so any inforamtion you could give me on how to create a macro in word and/or examples or scripts you've come across would be greatly appreciated.

12 replies

by rob miners

Pressing F1 while in word and lookup "Macro" will give you details. Below is a sample.

Sub delall()
'
' delall Macro
' Macro recorded 2/12/2005 by rrjmin0
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Wi-Fi "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

by eraser3498

That's fine if I just wanted to replace the word with a blank space but I want to be able to delete lines containing the word. Thanks for the input though

by bschaettle

This isn't a complete solution, but here's some code I have that will clear the current line:

Sub ClearCurrentLine()
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, _
Count:=1, Extend:=wdExtend
If Selection.Characters.Count = 1 And _
Asc(Selection.Characters(1)) = 13 Then Exit Sub
On Error Resume Next
Selection.Delete Unit:=wdCharacter, Count:=1
On Error GoTo 0
End Sub

by rkelly

Is this a list or along the same lines? I ask cause this might be a lot quicker with Excel if its not a Formatted Document (indentions, justifications, outlines, indexes)

by aakash shah

Another simpler, yet very effective approach would be to use the find and replace tool in MS Word. For example, if the word you are trying to remove is "bad_word". Then, click Edit | Replace and type in "bad_word" into the Find What field. Keep the Replace With field blank and then click Replace All. Note that you can choose more advanced options to choose advanced pieces of text also by clicking on More in the Find and Replace window.

Hope this helps.

by bschaettle

OK, here's a complete solution:

Sub ClearLines()
'
' ClearLines Macro
'
Dim sTargetText As String
Dim bStringFound As Boolean

sTargetText = InputBox("Please enter the text you want deleted", "title")

If Len(sTargetText) = 0 Then Exit Sub

Do
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = sTargetText
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

bStringFound = Selection.Find.Execute
If bStringFound Then
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.TypeParagraph
Selection.TypeParagraph
End If
Loop While bStringFound

End Sub ' ClearLines

This discussion is closed to new replies.