Share via

Word To Complex

Anonymous
2023-07-29T00:49:28+00:00

Hello from Steve

I am trying to Highlight "Barry Lockwood (Eagle Farm)"

Running the Macro it says

The Find What text contains a Pattern Match expression which is too complex

What is required please for the below to function. I thank you.

Sub Test()

Application.ScreenUpdating = False

Do

Selection.Find.ClearFormatting 

Selection.Find.Replacement.ClearFormatting 

With Selection.Find 

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

    .Replacement.Text = " " 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = False 

    .MatchWholeWord = False 

    .MatchWildcards = True 

    .MatchSoundsLike = False 

    .MatchAllWordForms = False 

End With 

Selection.Find.Execute 

If Selection.Find.Found Then 

    With Selection.Font 

    .Bold = True 

    .Color = wdColorRed 

End With 

Selection.EscapeKey 

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

Selection.HomeKey Unit:=wdLine 

End If 

Loop While Selection.Find.Found 

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

Anonymous
2023-07-29T01:25:47+00:00

Hi,

I'm Tim, an Independent Consultant here and a Microsoft user like you. I don't work for Microsoft and do not have access to any of your data on their system.

It seems that the macro you are trying to run has an issue with the Find What text, specifically with the pattern match expression. The expression provided seems to have some syntax errors. To fix it, you need to correct the regular expression. Here's the updated expression:-

Sub Test
    Application.ScreenUpdating = False
    Do
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            . Text = "[A-Z][a-z]{1,3} [A-Z][a-z]{1,} [A-Z][a-z]{1,31}"
            . Replacement.Text = " "
            . Forward = True
            . Wrap = wdFindStop
            . Format = True
            . MatchCase = False
            . MatchWholeWord = False
            . MatchWildcards = True
            . MatchSoundsLike = False
            . MatchAllWordForms = False
        End With
        Selection.Find.Execute
        If Selection.Find.Found Then
            With Selection.Font
                . Bold = True
                . Color = wdColorRed
            End With
            Selection.EscapeKey
            Selection.MoveDown Unit:=wdLine, Count:=2
            Selection.HomeKey Unit:=wdLine
        End If
    Loop While Selection.Find.Found
    Application.ScreenUpdating = True
End Sub

Now the macro should function properly, and it will find and format the specified pattern in your document.

I hope this information helps. If you have any questions, please let me know and I'll be glad to assist you further or If you find it helpful, you can mark this comment as the answer.

Kind regards

Tim

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-07-29T04:37:24+00:00

    That’s odd. However try the below code:-

    Sub Test
        Application.ScreenUpdating = False
        Do
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                . Text = "[A-Z][a-z]+ [A-Z][a-z]+ \([^\)]+\)"
                . Replacement.Text = " "
                . Forward = True
                . Wrap = wdFindStop
                . Format = True
                . MatchCase = False
                . MatchWholeWord = False
                . MatchWildcards = True
                . MatchSoundsLike = False
                . MatchAllWordForms = False
            End With
            Selection.Find.Execute
            If Selection.Find.Found Then
                With Selection.Font
                    . Bold = True
                    . Color = wdColorRed
                End With
                Selection.EscapeKey
                Selection.MoveDown Unit:=wdLine, Count:=2
                Selection.HomeKey Unit:=wdLine
            End If
        Loop While Selection.Find.Found
        Application.ScreenUpdating = True
    End Sub
    

    If this fails to function correctly then I’m unsure as to why it’s still throwing an error. I’ll await your update.

    Kind regards

    Tim

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-07-29T02:05:59+00:00

    Hello Tim

    Thank you

    What is happening is when I go to execute the macro.

    It is giving me an

    Compile error:

    Syntax error

    Also [A-Z][a-z]{1,3} is out of Range

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-07-29T01:48:53+00:00

    It seems that the regular expression in the code might not be capturing the complete name "Barry LockWood (Eagle Farm)" as expected. Let's modify the regular expression pattern to accommodate names with spaces and parentheses. Here's the updated code:

    Sub Test
        Application.ScreenUpdating = False
        Do
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                . Text = "[A-Z][a-z]{1,3} [A-Z][a-z]{1,} \(.\*\)"
                . Replacement.Text = " "
                . Forward = True
                . Wrap = wdFindStop
                . Format = True
                . MatchCase = False
                . MatchWholeWord = False
                . MatchWildcards = True
                . MatchSoundsLike = False
                . MatchAllWordForms = False
            End With
            Selection.Find.Execute
            If Selection.Find.Found Then
                With Selection.Font
                    . Bold = True
                    . Color = wdColorRed
                End With
                Selection.EscapeKey
                Selection.MoveDown Unit:=wdLine, Count:=2
                Selection.HomeKey Unit:=wdLine
            End If
        Loop While Selection.Find.Found
        Application.ScreenUpdating = True
    End Sub
    

    This modified pattern, "[A-Z][a-z]{1,3} [A-Z][a-z]{1,} \(.*\)", should now correctly highlight names like "Barry LockWood (Eagle Farm)" in your document. Please try running the updated macro, and it should work as expected.

    I hope this information helps. If you have any questions, please let me know and I'll be glad to assist you further or If you find it helpful, you can mark this comment as the answer.

    Kind regards

    Tim

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-07-29T01:45:21+00:00

    Hello Tim

    I thank you for your time on my issue.

    Yes it is looping but not Highliting "Barry Lockwood (Eagle Farm)"

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments