![]() |
|
|
|
#1
|
|||
|
|||
|
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”. |
|
#2
|
|||
|
|||
|
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
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Find and Replace not capturing entire line
|
Steve Kunkel | Word VBA | 7 | 04-22-2021 10:00 AM |
Capturing numbered headings
|
jbvalen | Word VBA | 5 | 05-04-2017 05:03 PM |
Capturing Redirected URL with macro
|
souravkp | Word VBA | 1 | 07-11-2015 09:34 PM |
Capturing addresses into contacts
|
lordnacho | Outlook | 1 | 11-01-2010 06:05 PM |
| time capturing | aligahk06 | Excel | 0 | 04-18-2010 11:53 PM |