#1
|
|||
|
|||
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 Help, please! |
#2
|
|||
|
|||
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? |
#3
|
|||
|
|||
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:
Thanks! |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
Quote:
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. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
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 |
Convert RegEx to Word (Devanagari Font Find/Replace) | gasyoun | Word VBA | 9 | 04-12-2013 04:15 PM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |