Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2025, 08:03 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default problem with capturing group in findText/ReplaceWith

Hello, I have the code below but since a few days the reuse of the group in parenthesis is misinterpreted in the ReplaceWith. For example if I have the following text “...Hello” I will get “...H ello”, with a space after the H instead of before.

If I go into word and do Ctrl+H, check wildcards and paste in search field “^0133([A-Za-zÀ-ÖØ-öø-ÿ0-9])” and replace field “... \1” I get the same error.

Has a parameter in my VBA changed or in Word these days? I've reinstalled Word365 twice, I've done the Office365 updates. It seems to me that this problem appeared after the last Windows 11 update.

Code:
Do 
    positionInitial = text.start
    text.Find.ClearFormatting
    text.Find.Replacement.ClearFormatting
    text.Find.MatchWildcards = True
    texte.Find.Execute findText:=“^0133([A-Za-zÀ-ÖØ-öø-ÿ0-9])”, ReplaceWith:=“... \1”, Replace:=2
Loop While texte.start <> positionInitial


Last edited by Ddadoo57; 02-18-2025 at 02:39 AM.
Reply With Quote
  #2  
Old 02-18-2025, 02:30 AM
macropod's Avatar
macropod macropod is online now problem with capturing group in findText/ReplaceWith Windows 10 problem with capturing group in findText/ReplaceWith Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I am not seeing that behaviour.

Kindly don't post the same question in multiple threads. I have deleted your other post.

When posting code, please post formatted code and use the code tags, which are accessed via the # symbol on the posting menu.

PS: Your Find/Replace code shouldn't have the smart quotes and it's not apparent why you're using a loop for something so simple. And, on the .Find.Execute line, you're referencing the undefined 'texte'.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-18-2025, 03:30 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

Thanks for the feedback, it's really appreciated!

Thanks for deleting the post in the other thread, I thought I had.

The texte variable is a Range and its content is set from an objDoc.content.

To tell you the truth, I process rtf files generated by DeepL where I correct any remaining punctuation errors. To do this, I process every punctuation mark in the document, checking that non-breaking spaces, single spaces, etc. are correctly placed before or after the punctuation, depending on the context of it. I also correct formatting errors specific to the formatting that must be respected.

The loop here comes from the fact that not all matches were processed. I have the same error whitout le loop

Code:
    texte.Find.ClearFormatting
    texte.Find.Replacement.ClearFormatting
    texte.Find.MatchWildcards = True
    texte.Find.Execute findText:="…([A-Za-zÀ-ÖØ-öø-ÿ0-9])", ReplaceWith:="… \1", Replace:=2

It would be easier to do the processing with regular expressions, but the problem was that I was losing the original formatting of the text contained in the rtf. In fact, I used to do this very well with just a few lines of code, but on plain text, my code was like this :
Code:
regex.Pattern = “(…)([A-Za-zÀ-ÖØ-öø-ÿ])”
regex.Global = True
text.Text = regex.Replace(text.Text, “$1 $2”)
Today, to avoid losing the formatting with a regex, I use this type of code, but not everything is processed as I'd like.

Code:
               regex.Pattern = “([A-Za-zÀ-ÖØ-öø-ÿ0-9])\.([A-Za-zÀ-ÖØ-öø-ÿ0-9])”
                regex.Global = True
                If regex.test(texte.Text) Then
                    Set objMatches = regex.Execute(text.Text)
                    For Each objMatch In objMatches
                        With texte.Find
                            .ClearFormatting: .Replacement.ClearFormatting: .MatchWildcards = False
                            .Text = objMatch.Value
                            .Replacement.Text = Left(.Text, 1) & “. “ & Right(.Text, 1)
                            .Wrap = wdFindContinue: .Execute Replace:=wdReplaceAll
                        End With
                    Next
                    Set objMatches = Nothing
                End If
In short, since February 13 or 14, 2025, after updating something, whether it's Windows 11 or Word or some other setting on the PC, I've had to modify some original code.

Have you tried in word itself by doing Ctrl+H, then check wildcards and paste in search field “^0133([A-Za-zÀ-ÖØ-öø-ÿ0-9])” and replace field “... \1” if you get the same error? I've tried on 2 computers for “...Hello” I get “...H ello”.
Reply With Quote
  #4  
Old 02-18-2025, 04:14 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

... just to clarify, I use Office 365. So I tried on 2 computers with Office 365 family and it gives the same error.

However, just now I tried on my wife's computer with Office 2016 pro plus and Office 365 Apps for Entreprise, and it works fine !!!!

So it's coming from Office 365 family, probably from the last update of February 2025



Does anyone know how to report this error to Miscrosoft so that it can be corrected?
Reply With Quote
  #5  
Old 02-18-2025, 05:54 AM
batman1 batman1 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2013
Advanced Beginner
 
Join Date: Jan 2025
Posts: 57
batman1 is on a distinguished road
Default

Quote:
Originally Posted by Ddadoo57 View Post
Today, to avoid losing the formatting with a regex, I use this type of code, but not everything is processed as I'd like.

Code:
               regex.Pattern = “([A-Za-zÀ-ÖØ-öø-ÿ0-9])\.([A-Za-zÀ-ÖØ-öø-ÿ0-9])”
                regex.Global = True
                If regex.test(texte.Text) Then
                    Set objMatches = regex.Execute(text.Text)
                    For Each objMatch In objMatches
                        With texte.Find
                            .ClearFormatting: .Replacement.ClearFormatting: .MatchWildcards = False
                            .Text = objMatch.Value
                            .Replacement.Text = Left(.Text, 1) & “. “ & Right(.Text, 1)
                            .Wrap = wdFindContinue: .Execute Replace:=wdReplaceAll
                        End With
                    Next
                    Set objMatches = Nothing
                 End If

Only regex without Find/Replace
Code:
Sub demo()
Const char = "…"
Dim i As Long, text As String, regex As Object, objMatch As Object, objMatches As Object
    text = ThisDocument.Range.text
    Set regex = CreateObject("VBScript.RegExp")
    regex.Global = True
    regex.Pattern = "(" & char & ")([A-Za-zA-ÖO-öo-y0-9])"
    If regex.test(text) Then
        Set objMatches = regex.Execute(text)
        For i = objMatches.Count - 1 To 0 Step -1
            Set objMatch = objMatches(i)
            ActiveDocument.Range(objMatch.FirstIndex, objMatch.FirstIndex + objMatch.Length).text = objMatch.SubMatches(0) & " " & objMatch.SubMatches(1)
        Next
    End If
    Set regex = Nothing
End Sub
Reply With Quote
  #6  
Old 02-18-2025, 01:52 PM
macropod's Avatar
macropod macropod is online now problem with capturing group in findText/ReplaceWith Windows 10 problem with capturing group in findText/ReplaceWith Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Frankly, I still can't see what you need the loop for. The same result could be achieved with nothing more than:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Execute FindText:="…([A-Za-zÀ-ÖØ-öø-ÿ0-9])", ReplaceWith:="... \1", _
    MatchWildcards:=True, Format:=False, Wrap:=wdFindContinue, Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
The only reason I can think of that you might be getting something like '...H ello' is if there is a tracked change at that point.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 02-19-2025, 12:23 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

Thanks for your feedback!

The behavior of my code is very curious for me too, knowing that group capture would work just fine before last February 13. I know that on that day I had updating windows 11 and office 365.

Usually, I get around the problem with the following code:

Code:
    With texte 
        With .Find
            .Forward = True: .Wrap = wdFindStop: .MatchWildcards = True
            .Text = "…[A-Za-zÀ-ÖØ-öø-ÿ0-9]"
        End With
        Do While .Find.Execute = True
            .Text = "… " & Right(.Text, 1): .Collapse wdCollapseEnd
        Loop
    End With
Anyway, many thanks for your contributions... if I find the reasons for this problem I'll come back to this post.
Reply With Quote
  #8  
Old 02-19-2025, 12:48 AM
macropod's Avatar
macropod macropod is online now problem with capturing group in findText/ReplaceWith Windows 10 problem with capturing group in findText/ReplaceWith Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Again, I can see no reason for having the loop.

With the code I posted, you might do better using:
…([A-Za-zÀ-ÖØ-öø-ÿ0-9]*>)
for the Find text
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-19-2025, 07:25 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

Thanks for your interest in my post!

You can forget about the loop because it's the same thing without a loop and I was using it in a special case.

I'm attaching all the following code that creates errors. You can see your code there, and in this case the error changes place a little.
In my rtf file I simply have a “Hi ...Hello!” after processing I have “Hi H... ello!”.


Code:
Sub M2_RTF_CorrectionPonctuation_WIP()    
	Dim objWord As Object
    Dim objDoc As Object
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim dossier As String
    Dim texte As Range
	
	With Application.fileDialog(msoFileDialogFolderPicker)
        .Title = "Sélectionnez le dossier contenant les fichiers RTF"
        .InitialFileName = ActiveDocument.Path & "\rtfs\" '"." 'dossierParDefaut 
        If .Show = -1 Then
            dossier = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(dossier)

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False 
    
    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "rtf" Then
            nbrFilesToDo = nbrFilesToDo + 1
        End If
    Next
    
    If nbrFilesToDo < 1 Then
        objWord.Quit
        Set objWord = Nothing
        Set objFolder = Nothing
        Set objFSO = Nothing
        
        MsgBox "Pas de fichiers RTF à traiter dans le dossier sélectionné !", vbInformation        
        Exit Sub
    End If

    Set regex = CreateObject("VBScript.RegExp")

    For Each objFile In objFolder.Files
    
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "rtf" Then
        
            Set objDoc = objWord.Documents.Open(objFile.Path, ReadOnly:=False)             

            objDoc.TrackRevisions = True
            
            Set texte = objDoc.content
			
			With texte.Find
			  .ClearFormatting
			  .Replacement.ClearFormatting
			  .Execute FindText:="...", ReplaceWith:="…", MatchWildcards:=True, format:=False, Wrap:=wdFindContinue, Replace:=wdReplaceAll
			End With

			Application.ScreenUpdating = False
			With texte.Find
			  .ClearFormatting
			  .Replacement.ClearFormatting
			  .Execute FindText:="…([A-Za-zÀ-ÖØ-öø-ÿ0-9])", ReplaceWith:="… \1", MatchWildcards:=True, format:=False, Wrap:=wdFindContinue, Replace:=wdReplaceAll
			End With
			Application.ScreenUpdating = True			
            
            objDoc.Save
            objDoc.Close False
        End If
    Next objFile
    
    objWord.Quit

    Set objDoc = Nothing
    Set objWord = Nothing
    Set objFile = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing    
    Set regex = Nothing
End Sub
Attached Images
File Type: png Hello1.png (4.3 KB, 20 views)
File Type: png Hello2.png (5.1 KB, 20 views)
Reply With Quote
  #10  
Old 02-19-2025, 07:48 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

... with just this code, the replacement gives “Hi ...H ello!”

So group captures can no longer be used in my code.

Code:
With texte.Find
    .Text = "…([A-Za-zÀ-ÖØ-öø-ÿ0-9])"
    .Replacement.Text = "… \1"
    .MatchWildcards = True: .Wrap = wdFindContinue: .Execute Replace:=wdReplaceAll
End With
Reply With Quote
  #11  
Old 02-19-2025, 02:54 PM
macropod's Avatar
macropod macropod is online now problem with capturing group in findText/ReplaceWith Windows 10 problem with capturing group in findText/ReplaceWith Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Your screenshot - and your code's objDoc.TrackRevisions = True - shows that you are using tracked changes! Which is what I mentioned in post #6...

You have also ignored my suggestion to use:
…([A-Za-zÀ-ÖØ-öø-ÿ0-9]*>)
for the Find text
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 02-19-2025, 11:33 PM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

No, no, I didn't ignore your suggestion, I tested it right away but it didn't work in my vba code.
Now I've just tried it again, I didn't set the revision tracking again, but it gives nothing, no replacement is made.

I've just run several tests in Word with Ctrl+H and this is what I get:
- if I remove the track changes and the cursor isn't between the ... and the H (Hi...|Hello!) the capture and replacement works fine. If it's between the ... and the H, nothing is found and replaced.
- if I leave the modification tracker on, the replacement is nonsense (HiHello...!).

So we can probably deduce that track changes have something to do with it, but I don't know how to do anything more in my code than use :

Code:
            
            objDoc.TrackRevisions = false 'Not objDoc.TrackRevisions
            objDoc.AcceptAllRevisions
Reply With Quote
  #13  
Old 02-20-2025, 02:07 AM
macropod's Avatar
macropod macropod is online now problem with capturing group in findText/ReplaceWith Windows 10 problem with capturing group in findText/ReplaceWith Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, use:
…([A-Za-zÀ-ÖØ-öø-ÿ0-9]@>)
for the Find text, as in:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Execute FindText:="…([A-Za-zÀ-ÖØ-öø-ÿ0-9]@>)", ReplaceWith:="... \1", _
    MatchWildcards:=True, Format:=False, Wrap:=wdFindContinue, Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
This should prevent the words splitting if you're using Track Changes, since the whole word is replaced.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 02-20-2025, 02:53 AM
Ddadoo57 Ddadoo57 is offline problem with capturing group in findText/ReplaceWith Windows 11 problem with capturing group in findText/ReplaceWith Office 2021
Advanced Beginner
problem with capturing group in findText/ReplaceWith
 
Join Date: Feb 2023
Posts: 90
Ddadoo57 is on a distinguished road
Default

It works very now, thank you, but TrackRevisions must not be at true otherwise the replacement is wrong.

Isn't there a way to keep an eye on the modifications without having to compare the original document with the modified one? It's important for me to be able to do this.

PS: I replaced each time in your code ... (3 dots in a row) with … (the character)
Reply With Quote
  #15  
Old 02-20-2025, 02:55 AM
macropod's Avatar
macropod macropod is online now problem with capturing group in findText/ReplaceWith Windows 10 problem with capturing group in findText/ReplaceWith Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What do you mean by "the replacement is wrong"?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with capturing group in findText/ReplaceWith Find and Replace not capturing entire line Steve Kunkel Word VBA 7 04-22-2021 10:00 AM
problem with capturing group in findText/ReplaceWith Capturing numbered headings jbvalen Word VBA 5 05-04-2017 05:03 PM
problem with capturing group in findText/ReplaceWith Capturing Redirected URL with macro souravkp Word VBA 1 07-11-2015 09:34 PM
problem with capturing group in findText/ReplaceWith Capturing addresses into contacts lordnacho Outlook 1 11-01-2010 06:05 PM
time capturing aligahk06 Excel 0 04-18-2010 11:53 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft