![]() |
#1
|
|||
|
|||
![]() 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. |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
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”) 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 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”. |
#4
|
|||
|
|||
![]()
... 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? |
#5
|
|||
|
|||
![]() Quote:
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 |
#6
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
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 |
#8
|
||||
|
||||
![]()
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] |
#9
|
|||
|
|||
![]()
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 |
#10
|
|||
|
|||
![]()
... 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 |
#11
|
||||
|
||||
![]()
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] |
#12
|
|||
|
|||
![]()
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 |
#13
|
||||
|
||||
![]()
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
![]()
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) |
#15
|
||||
|
||||
![]()
What do you mean by "the replacement is wrong"?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Steve Kunkel | Word VBA | 7 | 04-22-2021 10:00 AM |
![]() |
jbvalen | Word VBA | 5 | 05-04-2017 05:03 PM |
![]() |
souravkp | Word VBA | 1 | 07-11-2015 09:34 PM |
![]() |
lordnacho | Outlook | 1 | 11-01-2010 06:05 PM |
time capturing | aligahk06 | Excel | 0 | 04-18-2010 11:53 PM |