Consulting

Results 1 to 6 of 6

Thread: Loop to end of document

  1. #1

    Loop to end of document

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Eleanor,

    It looks as if you're trying to delete whole paragraphs. Try:
    [vba]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[/vba]
    The above will delete any paragraph containing the 'LOAD-DATE' string.

    PS: When posting code, please use th 'VBA' tags (per the posting toolbar)
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Eleanor,

    Try:
    [VBA]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[/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    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

  6. #6
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •