PDA

View Full Version : Loop to end of document



egran2ca
12-27-2011, 03:46 PM
I have created a macro to remove several lines of text that are found 80 plus times in a multi-page document. I need the macro to loop and then stop at the end of the document (or when the text is no longer found). I've spent hours reading about loops and trying hints found on the web, but can't make it work, so I'd appreciate it if someone could help. The code is pasted below. I need to use this macro as a template to create additional macros to remove other bits of text, so I'd appreciate it being simple and easily replicable.

Many thanks,
Eleanor


Sub GMLoadremove()
'
' GMLoadremove Macro
'
'
With Selection.Find
.Text = "LOAD-DATE"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=8, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub

macropod
12-27-2011, 08:00 PM
Hi Eleanor,

It looks as if you're trying to delete whole paragraphs. Try:
Sub Demo()
Dim i As Integer
With ActiveDocument.Range
With .Find
.ClearFormatting
.Text = "LOAD-DATE"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
.Paragraphs(1).Range.Text = vbNullString
i = i + 1
.Find.Execute
Loop
End With
MsgBox i & " instances found."
End Sub
The above will delete any paragraph containing the 'LOAD-DATE' string.

PS: When posting code, please use th 'VBA' tags (per the posting toolbar)

egran2ca
12-27-2011, 11:42 PM
Hi Paul

Sorry about not using the VBA tags (my first posting).

Thanks for the code, but it is not a single paragraph that I am trying to delete. The count of 8 in the following line of my original code would extend the selection to 8 paragraphs of 1 line each: 'Selection.MoveDown Unit:=wdLine, Count:=8, Extend:=wdExtend'.

This is an example of the type of text to be removed (it is single-spaced with a paragraph mark at the end of each line):
LOAD-DATE: September 18, 2006

LANGUAGE: ENGLISH

GRAPHIC: Illustration

PUBLICATION-TYPE: Newspaper
I hope that clarifies what I need and I do hope you'll be able to help.

Thanks,
Eleanor

macropod
12-27-2011, 11:50 PM
Hi Eleanor,

Try:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Integer, Rng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Text = "LOAD-DATE"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set Rng = .Paragraphs(1).Range
Rng.MoveEnd wdParagraph, 7
Rng.Text = vbNullString
i = i + 1
.Find.Execute
Loop
End With
MsgBox i & " instances found."
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub

egran2ca
12-28-2011, 11:50 AM
Hi Paul

That worked like a charm. I really must sit down and learn how to create code, so that I don't have to cry for help on a relatively simple macro. In the meantime, I'm just grateful for people like you on the forum.

Thank you VERY MUCH!

Eleanor

HJ Norman
01-01-2012, 10:57 AM
Eleanor, I share your feelings. Often I have tried to learn VBA, but still am very bad at it. Worse, I don't have any good lead.
On being grateful...that's exactly what I feel.
Long live this forum!