Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2023, 03:18 AM
alex100 alex100 is offline Use RegEx and preserve the original text formatting Windows 7 64bit Use RegEx and preserve the original text formatting Office 2016
Advanced Beginner
Use RegEx and preserve the original text formatting
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Use RegEx and preserve the original text formatting


The script below uses RegEx to find and replace within a Word selection. The problem is that many times, depending how the text is formatted, it changes this formatting style.

How can I search and replace (using RegEx) while keeping everything else intact?

Thank you!

Alex

Code:
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.RegExp")
With RegEx
    .pattern = "old_text"
    .Global = True
    Selection.Text = .replace(Selection.Text, "new_text")
End With
Reply With Quote
  #2  
Old 02-24-2023, 01:59 PM
alex100 alex100 is offline Use RegEx and preserve the original text formatting Windows 7 64bit Use RegEx and preserve the original text formatting Office 2016
Advanced Beginner
Use RegEx and preserve the original text formatting
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Ok, in the meantime I managed to find a solution using wildcards.

Alex
Reply With Quote
  #3  
Old 02-25-2023, 02:09 PM
macropod's Avatar
macropod macropod is offline Use RegEx and preserve the original text formatting Windows 10 Use RegEx and preserve the original text formatting Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

Regex has no regard to formatting.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use RegEx and preserve the original text formatting Formatting figures ?? - Preserve formatting of shapes for future use ksor Word 4 11-04-2019 11:35 AM
Use RegEx and preserve the original text formatting Is it spossible to preserve the original line numbers for reference when editing a document? mr_gray Word 3 10-02-2015 05:53 AM
Use RegEx and preserve the original text formatting Need macro to merge files in Word and preserve formatting Carolin Word VBA 3 12-14-2014 04:01 AM
How to extract text from a cell and preserve special formatting? epid011 Word VBA 2 04-04-2014 04:54 PM
Use RegEx and preserve the original text formatting Cut and paste a range of cells and preserve formatting StarWeaver Excel 1 03-02-2010 01:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:12 AM.


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