Using VBScript Regex in Word VBA

Discussion in 'Word VBA Beginners' started by Poseur, Aug 22, 2004.

  1. Poseur

    Poseur Guest

    Someone might find this handy.
    Having gotten comfortable using the VBScript RegExp Object via
    COM in Excel, I wanted to use it also in Word just as a handy
    little precise and efficient search and replace utility. Could
    not find any suggestions on how to do that exactly. Googling
    these groups, I saw a lot of references to Graham Mayor's
    article "Finding and Replacing Characters Using Wildcards"
    (http://www.mvps.org/word/FAQs/General/UsingWildcards.htm) but
    after I read it it seemed I'd have to learn 2 sets of meta-
    characters, a different behavior (non-greedy by default) and
    the wildcard set does not offer any lookaround facility.
    So, I piddled and I finally got it. An interactive fxn
    (procedure) I put in my custom menu. It is very quick and I
    can be very precise:

    For a Selection:
    Sub RegExInterSel()
    Dim regEx As VBScript_RegExp_55.RegExp
    Dim withWhat As String
    Dim newStr As String
    Set regEx = New VBScript_RegExp_55.RegExp
    With regEx
    .Pattern = InputBox("Replace What?")
    .Global = True
    .IgnoreCase = False
    End With
    withWhat = InputBox("With What?")
    If regEx.Test(Selection.Text) Then
    Selection.Text = regEx.Replace(Selection.Text,withWhat)
    End If
    End Sub

    For the whole document:
    Sub RegExInterAll()
    Dim regEx As VBScript_RegExp_55.RegExp
    Dim withWhat As String
    Dim Sentence As Range
    Set regEx = New VBScript_RegExp_55.RegExp
    With regEx
    .Pattern = InputBox("Replace What?")
    .Global = True
    .IgnoreCase = False
    End With
    withWhat = InputBox("With What?")
    For Each Sentence In ActiveDocument.Range.Sentences
    If regEx.Test(Sentence.Text) Then
    Sentence.Text = regEx.Replace(Sentence.Text, _
    withWhat)
    End If
    Next Sentence
    End Sub

    There are probly some inefficiences in the above, particular
    the latter using the Range object. Had trouble with that one.
    If I "replaced" using large Range text properties for the
    entire document, all formatting went away and I got a text
    file. This works.

    --
    I'm gonna let you go now.

    Poseur
    Be whoever you want to be...it's the 'net!
     
    Poseur, Aug 22, 2004
    #1
    1. Advertising

  2. Poseur

    Word Heretic Guest

    G'day Poseur <>,

    Have you done timer comparisons vs Selection.Find?

    Steve Hudson - Word Heretic
    Want a hyperlinked index? S/W R&D? See WordHeretic.com

    steve from wordheretic.com (Email replies require payment)


    Poseur reckoned:

    >Someone might find this handy.
    >Having gotten comfortable using the VBScript RegExp Object via
    >COM in Excel, I wanted to use it also in Word just as a handy
    >little precise and efficient search and replace utility. Could
    >not find any suggestions on how to do that exactly. Googling
    >these groups, I saw a lot of references to Graham Mayor's
    >article "Finding and Replacing Characters Using Wildcards"
    >(http://www.mvps.org/word/FAQs/General/UsingWildcards.htm) but
    >after I read it it seemed I'd have to learn 2 sets of meta-
    >characters, a different behavior (non-greedy by default) and
    >the wildcard set does not offer any lookaround facility.
    >So, I piddled and I finally got it. An interactive fxn
    >(procedure) I put in my custom menu. It is very quick and I
    >can be very precise:
    >
    >For a Selection:
    >Sub RegExInterSel()
    > Dim regEx As VBScript_RegExp_55.RegExp
    > Dim withWhat As String
    > Dim newStr As String
    > Set regEx = New VBScript_RegExp_55.RegExp
    > With regEx
    > .Pattern = InputBox("Replace What?")
    > .Global = True
    > .IgnoreCase = False
    > End With
    > withWhat = InputBox("With What?")
    > If regEx.Test(Selection.Text) Then
    > Selection.Text = regEx.Replace(Selection.Text,withWhat)
    > End If
    >End Sub
    >
    >For the whole document:
    >Sub RegExInterAll()
    > Dim regEx As VBScript_RegExp_55.RegExp
    > Dim withWhat As String
    > Dim Sentence As Range
    > Set regEx = New VBScript_RegExp_55.RegExp
    > With regEx
    > .Pattern = InputBox("Replace What?")
    > .Global = True
    > .IgnoreCase = False
    > End With
    > withWhat = InputBox("With What?")
    > For Each Sentence In ActiveDocument.Range.Sentences
    > If regEx.Test(Sentence.Text) Then
    > Sentence.Text = regEx.Replace(Sentence.Text, _
    >withWhat)
    > End If
    > Next Sentence
    >End Sub
    >
    >There are probly some inefficiences in the above, particular
    >the latter using the Range object. Had trouble with that one.
    >If I "replaced" using large Range text properties for the
    >entire document, all formatting went away and I got a text
    >file. This works.
     
    Word Heretic, Aug 27, 2004
    #2
    1. Advertising

  3. Poseur

    Poseur Guest

    Word Heretic <> wrote in
    news::

    > G'day Poseur <>,
    >
    > Have you done timer comparisons vs Selection.Find?


    Sir Heretic,

    Don't even know how. I am an authentic Poseur, truly not the
    real deal. When I say it is fast, I mean simply that for my
    purposes and relatively small documents, it is as quick as I
    need. And it is faster than the built-in "wildcards" system
    because I already know the regex metas and syntax and don't have
    to think in a different system.
    Do have one correction to my original. The VBScript regex object
    syntax does not include look-arounds (my bad). .NET has a rich
    regex object on a par with Perl but requires access to the .NET
    objects.
    I'm trying to figure out how to wrap the open source PCRE (Perl
    Compatible Regular Expressions)as a COM object - anyone have any
    ideas, let me know

    --
    I'm gonna let you go now.

    Poseur
    Be whoever you want to be...it's the 'net!
     
    Poseur, Aug 30, 2004
    #3
  4. Poseur

    Word Heretic Guest

    G'day Poseur <>,

    StartTime = TimeValue(Now)
    .....
    ElapsedTime = TimeValue(Now) - StartTime



    Steve Hudson - Word Heretic
    Want a hyperlinked index? S/W R&D? See WordHeretic.com

    steve from wordheretic.com (Email replies require payment)


    Poseur reckoned:

    >Word Heretic <> wrote in
    >news::
    >
    >> G'day Poseur <>,
    >>
    >> Have you done timer comparisons vs Selection.Find?

    >
    >Sir Heretic,
    >
    >Don't even know how. I am an authentic Poseur, truly not the
    >real deal. When I say it is fast, I mean simply that for my
    >purposes and relatively small documents, it is as quick as I
    >need. And it is faster than the built-in "wildcards" system
    >because I already know the regex metas and syntax and don't have
    >to think in a different system.
    >Do have one correction to my original. The VBScript regex object
    >syntax does not include look-arounds (my bad). .NET has a rich
    >regex object on a par with Perl but requires access to the .NET
    >objects.
    >I'm trying to figure out how to wrap the open source PCRE (Perl
    >Compatible Regular Expressions)as a COM object - anyone have any
    >ideas, let me know
     
    Word Heretic, Aug 30, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Jenkins
    Replies:
    2
    Views:
    67
    Dave Jenkins
    Oct 14, 2005
  2. Geoff Cox

    a VBA RegEx question

    Geoff Cox, Aug 14, 2006, in forum: PowerPoint Newsgroup
    Replies:
    4
    Views:
    70
    Geoff Cox
    Aug 16, 2006
  3. Poseur

    Using VBScript Regex in Word VBA

    Poseur, Aug 22, 2004, in forum: Word VBA
    Replies:
    3
    Views:
    72
    Word Heretic
    Aug 30, 2004
  4. Poseur

    sing VBScript Regex in Word VBA

    Poseur, Aug 22, 2004, in forum: Word VBA
    Replies:
    0
    Views:
    67
    Poseur
    Aug 22, 2004
  5. jalanford
    Replies:
    6
    Views:
    77
    jalanford
    Aug 1, 2006
Loading...

Share This Page