General discussion

Locked

Macro in MS word for deleting lines

By eraser3498 ·
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.

This conversation is currently closed to new comments.

12 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by eraser3498 In reply to Macro in MS word for dele ...

Point value changed by question poster.

Collapse -

by Jacky Howe In reply to Macro in MS word for dele ...

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

Collapse -

by eraser3498 In reply to

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

Collapse -

by bschaettle In reply to Macro in MS word for dele ...

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

Collapse -

by eraser3498 In reply to

Poster rated this answer.

Collapse -

by rkelly In reply to Macro in MS word for dele ...

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)

Collapse -

by eraser3498 In reply to

Poster rated this answer.

Collapse -

by Aakash Shah In reply to Macro in MS word for dele ...

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.

Collapse -

by eraser3498 In reply to

Poster rated this answer.

Collapse -

by bschaettle In reply to Macro in MS word for dele ...

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

Back to Software Forum
12 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums