Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to exit macro if selection.copy not executble

    When I run this macro, I get an error message when the macro does not find any text to copy at the selection. copy line. I need a way to exit out of the macro instead of the macro generating an error when the macro does not find anything to copy.

    Here is an example of the document to be processed.

    <hr>
    Text line 1
    Text line 2
    Text line 3
    Text line 4
    FillInPreviousNumHere
    Text line 5
    Text line 6
    Text line 7
    Text line 8
    FillInPreviousNumHere
    Text line 9

    StartPreviousNumberLine
    222
    223
    <hr>

    <hr>
    Sub FillInPreviousNumbers()

    '
    Selection.Find.ClearFormatting
    Do
    With Selection.Find
    .Text = "StartPreviousNumberLine"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Copy
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "FillInPreviousNumHere"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    If Not Selection.Find.Execute Then Exit Do
    'Selection.Find.Execute
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Paste
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Loop
    End Sub
    <hr>

    Thank you in advance for your replies.

  2. #2
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to exit macro if selection.copy not execu

    The error I am receiving is Runtime error 4605.

    Thank you.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need to exit macro if selection.copy not executble

    Try this version:

    Sub FillInPreviousNumbers()
    With Selection.Find
    .ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    Do
    .Text = "StartPreviousNumberLine"
    .Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=2
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    If Len(Selection) < 2 Then Exit Do
    Selection.Copy
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.HomeKey Unit:=wdStory
    .Text = "FillInPreviousNumHere"
    If Not .Execute Then Exit Do
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Paste
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Loop
    End With
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Need to exit macro if selection.copy not executble

    Try replacing
    <code>Selection.Find.Execute</code>
    with
    <code>If selection.Find.Execute</code>
    and then put
    <code>End If</code> immediately after <code>End With</code> and before the next <code>With Selection.Find</code>

    StuartR

  5. #5
    Star Lounger
    Join Date
    Jul 2008
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need to exit macro if selection.copy not execu

    Thank you Hans for the suggestion. The macro works fine and does not kick out an error message.

Posting Permissions

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