Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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: 94
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
 



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 06:06 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