Share via

Word Do While .Execute

Anonymous
2024-05-02T19:12:32+00:00

Hello from Steve

Objective is to select R52 then bold and colour blue

What is happening it selects Do While .Execute and stops.

What is required please for the below to function.

Sub Colour_Ratings()

Application.ScreenUpdating = False 

Selection.HomeKey Unit:=wdStory 

With Selection.Find 

    .ClearFormatting 

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

    .Forward = True 

    .Wrap = wdFindStop 

    .Format =True 

    .MatchCase = False 

    .MatchWholeWord = False 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

    Do While .Execute 

     Selection.Collapse Direction:=wdCollapseEnd 

     Selection.MoveEndUntil " " 

     Selection.Font.Bold = True 

     Selection.Font.Color = wdColorBlue 

     Selection.Collapse Direction:=wdCollapseEnd 

    Loop 

End With 

Application.ScreenUpdating = True 

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

HansV 462.6K Reputation points
2024-05-02T20:01:02+00:00

I created a small test document:

I ran your macro as posted, without any modification:

It appears to work, so perhaps there is something in your document that the code doesn't take into account.

If you cannot find what causes the problem yourself, could you make a document available that demonstrates the problem?

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-05-02T20:15:38+00:00

    Hello HansV

    I find it interesting the sample Sub_Colour_Breeding works perfectly on the same document.

    You have done me a great favour by saying it is working for you.

    Interesting to say the least.

    I thank you for your time and effort.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-05-02T19:50:57+00:00

    Hello HansV

    Thank you

    In a nutshell Yes

    I have now achieved the script to select R52, R59, R60 and so on., from start of document to the end of document.

    The issue now once selected it goes to the next occurence without bolding and coloring blue

    I would be happy with Green

    Steve

    ps the below works asit it should

    Sub Colour_Breeding()

    Application.ScreenUpdating = False 
    
    Selection.HomeKey Unit:=wdStory 
    
    With Selection.Find 
    
        .ClearFormatting 
    
        .Text = "(by" 
    
        .Forward = True 
    
        .Wrap = wdFindStop 
    
        .Format = False 
    
        .MatchCase = True 
    
        .MatchWholeWord = False 
    
        .MatchWildcards = False 
    
        .MatchSoundsLike = False 
    
        .MatchAllWordForms = False 
    
        Do While .Execute 
    
            Selection.MoveEndUntil ")" 
    
            Selection.Font.Bold = True 
    
            Selection.Font.Color = wdColorRed 
    
            Selection.Collapse Direction:=wdCollapseEnd 
    
        Loop 
    
    End With 
    
    Application.ScreenUpdating = True 
    
    End Sub
    

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2024-05-02T19:40:44+00:00

    The lines

             Selection.Collapse Direction:=wdCollapseEnd 
             Selection.MoveEndUntil " " 
    

    select the text after R52 (or similar) until a space, and formats that text bold and blue. So for example:

    This is R52blahblah etcetera. (I used green since the forum doesn't do blue)

    Is that what you intended?

    Was this answer helpful?

    0 comments No comments