Share via

Word " Do While .Execute"

Anonymous
2022-12-13T00:48:24+00:00

Hello from Steve

When Exexcuting the second time it starts the script at .Text = ")"

How do I please Have it starting at .Text = "(by"

Sub The_Second_Tidy_up()

Dim rng As Range 

Selection.Find.ClearFormatting 

With Selection.Find 

    .Text = "(by" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = False 

    .MatchCase = False 

    .MatchWholeWord = True 

    .MatchWildcards = False 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

     Do While .Execute 

    Selection.Extend 

    With Selection.Find 

    .Text = ")" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = False 

    .MatchCase = True 

    .MatchWholeWord = False 

    .MatchWildcards = False 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

    Selection.Find.Execute 

End With 

Selection.Delete 

Selection.Extend 

With Selection.Find 

    .Text = "[A-Z][a-z]{1,} [A-Z][A-z]{1,} \(\*\)" 

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = True 

    .MatchWholeWord = False 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

    .Execute 

    End With 

 With Selection.Find 

    .Text = " [A-Z]" 

    .Replacement.Text = "" 

    .Forward = False 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = False 

    .MatchWholeWord = False 

    .MatchAllWordForms = False 

    .MatchSoundsLike = False 

    .MatchWildcards = True 

End With 

Selection.Find.Execute 

Selection.Find.Execute 

Selection.Delete 

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.MoveRight Unit:=wdCharacter, Count:=1 

Selection.Extend 

With Selection.Find 

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

    .Replacement.Text = "" 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = True 

    .MatchWholeWord = False 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

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

Selection.Delete 

Selection.TypeParagraph 

Selection.Delete 

If Selection.End >= ActiveDocument.Content.End - 1 Then Exit Do 

    Loop 

End With 

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-12-13T14:05:24+00:00

Hi Steve8d,

It’s always my Pleasure to assist you, Sorry for the inconvenience caused It looks like the issue is with the Selection.Find.Text = "(by" line in your code. The Find method searches for a specified text string within the selected range, and the Text property sets or returns the text to be found.

In this case, it appears that the Text property is being set to "(by", which means that the Find method will search for this exact text string. However, it seems that the text in your document is not exactly "(by", but is instead something like "(by Author)". This means that the Find method will not find this text, and will instead move on to the next Find statement.

To fix this issue, you can modify the Text property to search for a more general pattern that will match the text in your document. For example, you could use the following regular expression to search for text that begins with "(by" and ends with ")":

. Text = "(by.*)$"

This regular expression will match any text that begins with "(by" and ends with ")", and will allow the Find` method to find the text in your document.

Additionally, it looks like there may be some unnecessary or redundant code in your script. For example, the Selection.Find.Execute method is called multiple times within the Do While loop, but this is not necessary because the Execute method is already called within the Do While condition. You may want to review your code and remove any unnecessary or redundant statements to improve its efficiency.

To remove unnecessary or redundant statements and improve the efficiency of your code, you can follow these steps:

  • Review your code carefully and identify any statements or blocks of code that do not appear to be necessary for the desired outcome of your script.
  • Remove these unnecessary or redundant statements from your code.
  • Test your code to ensure that it still produces the desired results.
  • Repeat these steps as needed until your code is efficient and free of unnecessary or redundant statements.

It's important to note that the specific steps for removing unnecessary or redundant statements will depend on the specific code you are working with. However, some general principles to keep in mind when trying to improve the efficiency of your code include:

  • Avoid repeating the same operations or calculations multiple times within a single script. Instead, try to write your code in a way that allows you to perform the operation or calculation once and then reuse the result later in the script.
  • Avoid using long, complex conditional statements or nested loops if possible. Instead, try to break your code into smaller, simpler pieces that can be combined to achieve the desired result.
  • Avoid using multiple, similar methods or functions to perform the same operation. Instead, try to find a single method or function that can be used to accomplish the task in a more efficient way.
  • Avoid using large, complex data structures if possible. Instead, try to use simpler data structures that are easier to work with and can be processed more efficiently by your code.

By following these principles, you can improve the efficiency of your code and make it easier to maintain and update in the future.

Here's the revised version of your Code :-

Sub The_Second_Tidy_up() Dim rng As Range

' Find text that matches "(by" and delete it With Selection.Find . Text = "(by" . Replacement.Text = "" . Forward = True . Wrap = wdFindStop . Format = True . MatchCase = True . MatchWholeWord = False . MatchWildcards = False . MatchSoundsLike = False . MatchAllWordForms = False

Do While Selection.Find.Execute And Selection.Find.Text = "(by" Selection.Extend Selection.Find.Execute Selection.Delete Loop End With

' Find text that matches ")" and delete it With Selection.Find . Text = ")" . Replacement.Text = "" . Forward = True . Wrap = wdFindStop . Format = True . MatchCase = True . MatchWholeWord = False . MatchWildcards = False . MatchSoundsLike = False . MatchAllWordForms = False

Do While Selection.Find.Execute And Selection.Find.Text = ")" Selection.Extend Selection.Find.Execute Selection.Delete Loop End With

It's too long rest part can be done by you from "Find text that matches a name followed by a number in parentheses, and delete it"

If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help to the best of my ability.

Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

Best Regards, Sneha

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-12-13T23:07:39+00:00

    Hello Sneha

    I thank you

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-12-13T02:52:58+00:00

    Hello Sneha

    Firstly thank you

    When I do an F8 from the start of the script to the end it operates as it should.

    However It is still not finding .Text = "(by"

    When push F5 it still goes to .Text = ")"

    Sub The_Second_Tidy_up()

    Dim rng As Range 
    
    Selection.Find.ClearFormatting 
    
    With Selection.Find 
    
        .Text = "(by" 
    
        .Replacement.Text = "" 
    
        .Forward = True 
    
        .Wrap = wdFindStop 
    
        .Format = True 
    
        .MatchCase = True 
    
        .MatchWholeWord = False 
    
        .MatchWildcards = False 
    
        .MatchSoundsLike = False 
    
        .MatchAllWordForms = False 
    
        Do While Selection.Find.Execute And Selection.Find.Text = "(by" 
    
        Selection.Extend 
    
        With Selection.Find 
    
        .Text = ")" 
    
        .Replacement.Text = "" 
    
        .Forward = True 
    
        .Wrap = wdFindStop 
    
        .Format = True 
    
        .MatchCase = True 
    
        .MatchWholeWord = False 
    
        .MatchWildcards = False 
    
        .MatchSoundsLike = False 
    
        .MatchAllWordForms = False 
    
        Selection.Find.Execute 
    
    End With 
    
    Selection.Delete 
    
    Selection.Extend 
    
    With Selection.Find 
    
        .Text = "[A-Z][a-z]{1,} [A-Z][A-z]{1,} \(\*\)" 
    
        .Replacement.Text = "" 
    
        .Forward = True 
    
        .Wrap = wdFindStop 
    
        .Format = True 
    
        .MatchCase = True 
    
        .MatchWholeWord = False 
    
        .MatchWildcards = True 
    
        .MatchSoundsLike = False 
    
        .MatchAllWordForms = False 
    
        .Execute 
    
        End With 
    
     With Selection.Find 
    
        .Text = " [A-Z]" 
    
        .Replacement.Text = "" 
    
        .Forward = False 
    
        .Wrap = wdFindStop 
    
        .Format = True 
    
        .MatchCase = False 
    
        .MatchWholeWord = False 
    
        .MatchAllWordForms = False 
    
        .MatchSoundsLike = False 
    
        .MatchWildcards = True 
    
    End With 
    
    Selection.Find.Execute 
    
    Selection.Find.Execute 
    
    Selection.Delete 
    
    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.MoveRight Unit:=wdCharacter, Count:=1 
    
    Selection.Extend 
    
    With Selection.Find 
    
        .Text = "[0-9]{1,}. " 
    
        .Replacement.Text = "" 
    
        .Forward = True 
    
        .Wrap = wdFindStop 
    
        .Format = True 
    
        .MatchCase = True 
    
        .MatchWholeWord = False 
    
        .MatchWildcards = True 
    
        .MatchSoundsLike = False 
    
        .MatchAllWordForms = False 
    
    End With 
    
    Selection.Find.Execute 
    
    Selection.MoveLeft Unit:=wdCharacter, Count:=4 
    
    Selection.Delete 
    
    Selection.TypeParagraph 
    
    Selection.Delete 
    
    If Selection.End >= ActiveDocument.Content.End - 1 Then Exit Do 
    
        Loop 
    
    End With 
    

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-12-13T01:45:11+00:00

    Hi Steve8d,

    I'm Sneha and I'd be happy to help you out with your question.

    In the The_Second_Tidy_up() subroutine, you can modify the Do While . Execute statement to include a condition that checks whether the . Text property of the Selection.Find object is equal to "(by". If it is not, you can use the Selection.Find.Execute method to search for the next occurrence of "(by". Here is an example of how you might modify the Do While statement:

    Do While Selection.Find.Execute And Selection.Find.Text = "(by" This will execute the loop body only if the Selection.Find.Execute method returns True and the . Text property of the Selection.Find object is equal to "(by". This will ensure that the loop body only executes for instances of "(by" in the document.

    If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help to the best of my ability.

    Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

    Best Regards, Sneha

    Was this answer helpful?

    0 comments No comments