Share via

Word "Do While Selection.Find.Execute = True".

Anonymous
2022-02-28T02:07:48+00:00

Hello from Steve

The below is ignoring the script ".Text = "[a-z]:"" meaning it will not execute.

What is required please to have the above reconize ".Text = "[a-z]:""

Apart from the above it is Working as it was intended.

To me the below is an excellent and brilliant "Loop". Thank you

Sub Jockey()

Do While Selection.Find.Execute = True 

Selection.Find.ClearFormatting 

With Selection.Find 

    .Text = "[a-z]:" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = False 

    .MatchCase = True 

    .MatchWholeWord = True 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

Selection.MoveRight Unit:=wdCharacter, Count:=3 

With Selection.Find 

    .Text = "[0-9]{1,} [A-Z]{1,}" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = True 

    .MatchWholeWord = True 

    .MatchAllWordForms = False 

    .MatchSoundsLike = False 

    .MatchWildcards = True 

End With 

Selection.Find.Execute 

Selection.Extend 

With Selection.Find 

    .Text = "  " 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = True 

    .MatchWholeWord = False 

    .MatchWildcards = False 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

Selection.MoveLeft Unit:=wdCharacter, Count:=2 

Selection.Fields.Update 

Dim strText As String 

strText = Selection.Text 

Selection.Collapse Direction:=wdCollapseStart 

Selection.HomeKey Unit:=wdStory 

Selection.MoveUp Unit:=wdLine, Count:=1 

Selection.Find.ClearFormatting 

With Selection.Find 

    .Forward = True 

    .Wrap = wdFindStop 

    .MatchCase = True 

    .MatchWholeWord = True 

    .Text = strText 

    .MatchWildcards = False 

    .Execute 

End With 

Selection.MoveRight Unit:=wdCharacter, Count:=1 

Selection.Find.ClearFormatting 

With Selection.Find 

    .Text = "[0-9]{1,}.[0-9]" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = False 

    .MatchCase = False 

    .MatchWholeWord = False 

    .MatchAllWordForms = False 

    .MatchSoundsLike = False 

    .MatchWildcards = True 

End With 

Selection.Find.Execute 

Selection.Find.Execute 

Selection.MoveLeft Unit:=wdCharacter, Count:=2 

Selection.Extend 

Selection.Find.ClearFormatting 

Selection.Find.Replacement.ClearFormatting 

With Selection.Find 

    .Text = "[A-Z] [A-Z]{1,}" 

    .Replacement.Text = "" 

    .Forward = False 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = True 

    .MatchWholeWord = False 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

Selection.Copy 

Selection.HomeKey Unit:=wdLine 

With Selection.Find 

    .Text = "[0-9]{1,} [A-Z]{1,}" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = False 

    .MatchCase = True 

    .MatchWholeWord = False 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

Selection.Fields.Update 

strText = Selection.Text 

Selection.Collapse Direction:=wdCollapseStart 

Selection.HomeKey Unit:=wdLine 

Selection.MoveDown Unit:=wdLine, Count:=4 

With Selection.Find 

    .Forward = True 

    .Wrap = wdFindStop 

    .MatchCase = True 

    .MatchWholeWord = True 

    .Text = strText 

    .MatchWildcards = False 

    .Execute 

End With 

Selection.MoveRight Unit:=wdCharacter, Count:=1 

With Selection.Find 

    .Text = "  " 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = False 

    .MatchCase = False 

    .MatchWholeWord = False 

    .MatchWildcards = False 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

Selection.MoveRight Unit:=wdCharacter, Count:=1 

Selection.Paste 

Selection.MoveDown Unit:=wdLine, Count:=1 

Selection.EndKey Unit:=wdLine 

Selection.HomeKey Unit:=wdLine 

Loop

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2022-02-28T10:55:22+00:00

If you want to run the macro as soon as you get to the function, try setting "Selection.Find.Execute" to false insted of True - but my guess if it will start an infinite loop, make sure to save your document before trying it. And you may end up force closing the Word from the Task Manager.

Best approach would be to use a boolean variable, set that boolean variable to true at the beginning and at the end of the loop to set the boolean variable to false:

Like this bFound = .Execute(Replace:=wdReplaceOne)

Refer this question:

https://stackoverflow.com/questions/49387914/wo...

Disclaimer:

This reply contains non-Microsoft websites. The page(s) appear to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-02-28T07:55:52+00:00

    Hello Bhavik

    Looking at the below for example the script looks for

    d:  10 VISTOCK

    once found it then goes to

    10 VISTOCK                          55X6850  (12)  56.5  K WILLIAMS

    Objective then is to copy K WILLIAMS

    finally paste it in the below

    3rd:  10 VISTOCK K WILLIAMS

    10 VISTOCK                          55X6850  (12)  56.5  K WILLIAMS

    3rd:  10 VISTOCK

    3rd:  10 VISTOCK K WILLIAMS

    9 ROYAL KHAN                   50500475545  (11)  56.5       B MAY

    9th:   9 ROYAL KHAN

    9th:   9 ROYAL KHAN B MAY

    4 ALLASERA                          304477  ( 3)  57.5K CHOWDHOORY

    2nd:   4 ALLASERA

    2nd:   4 ALLASERA  K CHOWDHOORY

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-02-28T07:01:34+00:00

    Oh okay, and it is not able to find only this text, when you use "[a-z]:", right? Rest all is working fine, being found and replaced too?

    If so, would you mind sharing the document with me (if it is not having any personal information or sensitive data) so that I can take a look at it on my end?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-02-28T06:59:12+00:00

    Hello Bhavik

    I thank you for taking timeout on my issue.

    In my Heading I Have the Month "Feb" hence your Script will find "e"

    The Reason for .Text = "[a-z]:" is that it should find for example

    1st: or 2nd: or 3rd: and so on.

    Sorry I should have expanded futher.

    All the best

    Steve

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2022-02-28T06:26:11+00:00

    Hi Steve8d,

    Sorry to hear that you're experiencing this issue!

    I am Bhavik, a Microsoft user like you. Please note, I am not a Microsoft employee and don’t have direct access to your account, but I will do everything I can to help you with your queries. :)

    Try replacing your code with this one and run it again please:

    .Text = "[a-z]"

    Let me know in case of any further assistance is required, I will be glad to assist.

    Thanks

    Bhavik

    Was this answer helpful?

    0 comments No comments