Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-30-2023, 09:33 AM
hoosier122 hoosier122 is offline Help Creating a Simple Find & Replace with REGEX in Word Windows 10 Help Creating a Simple Find & Replace with REGEX in Word Office 2021
Novice
Help Creating a Simple Find & Replace with REGEX in Word
 
Join Date: May 2023
Posts: 3
hoosier122 is on a distinguished road
Default Help Creating a Simple Find & Replace with REGEX in Word

Greetings! I simply want to be able to use Regex within a VB script for Word.

Surely someone already has this script already written?

No, I don't want to use Word's built-in Find & Replace function including its "Wildcards" feature.

Code:
Sub LineCounter()
Dim regExp As Object
Set regExp = CreateObject("vbscript.regexp")

With regExp
    .Pattern = "(^p^p)"
    .Global = True
    Selection.Text = .Replace(Selection.Text, "^p")
End With
End Sub
The above code kind of works, but I don't want to have to highlight text AND this code also unpredictably breaks the formatting of Word documents in which it is run.



Help, please!
Reply With Quote
  #2  
Old 05-30-2023, 07:31 PM
BrianHoard BrianHoard is offline Help Creating a Simple Find & Replace with REGEX in Word Windows 10 Help Creating a Simple Find & Replace with REGEX in Word Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Can you share the text you're trying find, and what you want to replace it with in a Word doc, rather than in your code? Just want to be sure I understand what you're trying to do, since the ^ character has special meaning in regex.
I see that you're also not wanting to use the built in find/replace. Please explain why. Like what does that do that you don't want?
Reply With Quote
  #3  
Old 05-31-2023, 01:36 PM
hoosier122 hoosier122 is offline Help Creating a Simple Find & Replace with REGEX in Word Windows 10 Help Creating a Simple Find & Replace with REGEX in Word Office 2021
Novice
Help Creating a Simple Find & Replace with REGEX in Word
 
Join Date: May 2023
Posts: 3
hoosier122 is on a distinguished road
Default

I want to be able to use all of Regex's features within Word because I do a lot of different types of document manipulation. For this particular feature, I want to be able to make a Line Numberer that outputs a number on every "non-blank" line.

I realize that you shouldn't have any "blank" lines in Word -- instead use paragraphing features like "Add Space Before Paragraph" and "Add Space After Paragraph," but I actually don't "own" these files and I am just making edits/corrections.

So, here would be an example (ignore content of the writing...):

Quote:

1 - Tracy Pope
2 - 123 Anywhere Street
3 - Somewhereville, Best State 88889

4 - January 1, 2010

5 - Mr. Ryan Francom
6 - ABC Company
7 - 456 Somewhere Road
8 - Best Town, Best State 88888



9 - Dear Mr. Francom:

10 - ABC Company has provided me with many wonderful opportunities to learn and grow professionally and personally. It is with difficulty that I submit my resignation from my position as Director of Marketing effective January 15, 2010.

Due to personal circumstances, we have chosen to relocate our family to the New York area which necessitates my leaving a position that I have enjoyed and loved the past 3 years.

I am thankful to ABC Company for the many wonderful opportunities and the great memories I will take with me. I would also like to thank you personally for the support over the years.

If I can be of any assistance during this transition, please do not hesitate to ask.

Sincerely,

[Signature]

Tracy Pope
[Title]
Line numbering is only one thing I want to be able to do. As I said, I want to be able to use all Regex functions within Word.

Thanks!
Reply With Quote
  #4  
Old 05-31-2023, 04:06 PM
Guessed's Avatar
Guessed Guessed is offline Help Creating a Simple Find & Replace with REGEX in Word Windows 10 Help Creating a Simple Find & Replace with REGEX in Word Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Your code is working on Selection.Text which is why it expects you to SELECT the text first. If you don't want to first select the text then you need to ask it to work on a range eg
ActiveDocument.Range.Text

Also, you are telling the code to replace the Text (which is an unformatted string of text) with another unformatted string of text - this is why your code is removing the formatting.

Some examples of others use of Regex in VBA might give you some hints to fix your code
Include All Stories in VBA RegEx Search in MS Word - Stack Overflow
microsoft word - Use RegEx and wildcard search to find a pattern - Super User
Using VBScript Regex in Word VBA

Are you really using vbscript or are you using VBA? It appears to be VBA in your example.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 06-01-2023, 05:50 AM
hoosier122 hoosier122 is offline Help Creating a Simple Find & Replace with REGEX in Word Windows 10 Help Creating a Simple Find & Replace with REGEX in Word Office 2021
Novice
Help Creating a Simple Find & Replace with REGEX in Word
 
Join Date: May 2023
Posts: 3
hoosier122 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Your code is working on Selection.Text which is why it expects you to SELECT the text first. If you don't want to first select the text then you need to ask it to work on a range eg
ActiveDocument.Range.Text

Also, you are telling the code to replace the Text (which is an unformatted string of text) with another unformatted string of text - this is why your code is removing the formatting.

Some examples of others use of Regex in VBA might give you some hints to fix your code
Include All Stories in VBA RegEx Search in MS Word - Stack Overflow
microsoft word - Use RegEx and wildcard search to find a pattern - Super User
Using VBScript Regex in Word VBA

Are you really using vbscript or are you using VBA? It appears to be VBA in your example.
Thank you so very much. I'll digest those examples later.

I'm using whatever language is in the Word menus when you click 'Visual Basic.' It says Visual Basic for Applications ... so I guess VBA?

Sorry, I know C++ and PHP, but never took the time to learn Basic, so I just try to piece it together when I need to work in VB. And of course I get the help of this forum!

Thanks again.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Creating a Simple Find & Replace with REGEX in Word Regex to find space after two-letter words at the of the line and replace Bachelar Word VBA 3 05-29-2022 07:03 AM
Help Creating a Simple Find & Replace with REGEX in Word Using VB Regex feature, I tried to replace 'the' and 'this' with 'that' but got screwed abdan Word VBA 3 01-18-2019 09:38 PM
In Find and Replace, can Word stop after each Replace? wardw Word 1 06-08-2017 02:47 PM
Help Creating a Simple Find & Replace with REGEX in Word Convert RegEx to Word (Devanagari Font Find/Replace) gasyoun Word VBA 9 04-12-2013 04:15 PM
Help Creating a Simple Find & Replace with REGEX in Word Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:20 PM.


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