2

Say I have the following string:

"Hello how are you."

Since MS Word allows for regular expressions, I can use "*" to find the complete string. But what if I want to exclude the delimiters (the quotes)? I'm afraid that MS Word doesn't support either of the two methods explained here. My question is: would there be any way to do this in one search query? Thanks in advance.

Community
  • 1
  • 1
Daan
  • 1,417
  • 5
  • 25
  • 40

5 Answers5

1

There are different ways to achieve what you want. Here is one way to find text in VBA Word without the dilimiters using Regex. Let's say you have the following text in Word Document (do not copy and paste it from here as the the website distorts the Double quotes. See the screenshot)

This is a sample

"This is another Sample"

"Wake me up before you go go"

"War of the worlds"

The code to return text using Regex between two quotes is as follows

Sub FindText()
      Dim regEx, Match, Matches

      Set regEx = New RegExp
      regEx.Pattern = "([^“]*)(?=\”)"

      regEx.IgnoreCase = False
      regEx.Global = True

      Set Matches = regEx.Execute(ActiveDocument.Range.Text)

      For Each Match In Matches
         Debug.Print Match.Value
      Next
End Sub

and if you want to say find "Wake me up before you go go" without quotes then you can use this as well

Sub FindText()
      Dim regEx, Match, Matches
      Dim searchText As String

      searchText = "Wake me up before you go go"
      Set regEx = New RegExp
      regEx.Pattern = "([^“]*)(?=\”)"

      regEx.IgnoreCase = False
      regEx.Global = True

      Set Matches = regEx.Execute(ActiveDocument.Range.Text)

      For Each Match In Matches
         If Trim(Match.Value) = (searchText) Then
            Debug.Print "Found"
         End If
      Next
End Sub

NOTE: The website distorts the actual double quote so I am posting screenshots.

enter image description here

FOLLOWUP

For the sample file that you posted, use this code

Sub FindText()
    Dim regEx, Match, Matches

    Set regEx = New RegExp
    regEx.Pattern = """([^""]*)"""
    regEx.IgnoreCase = False
    regEx.Global = True

    Set Matches = regEx.Execute(ActiveDocument.Range.Text)

    For Each Match In Matches
        Debug.Print Match.SubMatches(0)
    Next
End Sub

Sample File can be downloaded from here. Please note that this link will be active for 7 days.

Sample File

HTH

Sid

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Thanks for your elaborate answer. I´ve been able to add this to the project and compile it. However, when I run it nothing seems to happen. Could you give me a few steps on how to run this? Thanks. – Daan Mar 15 '12 at 00:12
  • I've created two [screenshots](http://www.dropbox.com/gallery/15185020/2/Public%20Album/MS%20Word%20-%20FindText?h=c4c848), which also include an example of the track listing. I hope it helps. – Daan Mar 15 '12 at 14:32
  • @KeyMs92: Yes that helps to some extent. One last request, can you upload that sample word file? Let me test it myself? – Siddharth Rout Mar 15 '12 at 14:34
  • Alright, [here](http://dl.dropbox.com/u/15185020/MS%20Word%20-%20FindText.docx) it is. – Daan Mar 15 '12 at 14:47
  • @KeyMs92: Hmm, I had guessed it correct :) You have different type of `Quotes` there. Let me get back to you on this after I finish playing with it. Will upload the sample file when done :) – Siddharth Rout Mar 15 '12 at 15:02
  • @KeyMs92: Please test it now :) – Siddharth Rout Mar 15 '12 at 16:44
  • Oh one small thing: Is there an easy way to refresh/clean the "Immediate" window when I run the method again? – Daan Mar 15 '12 at 18:24
  • Easy Way: Place your cursor in the immediate window and then press CTRL A and then press DELETE :) – Siddharth Rout Mar 15 '12 at 18:32
0

I have had luck using color (Format < Font in the Search dialog box while you are clicked into Find What) to solve problems like this. Execute search all content with delimiters (“*” with wildcards checked for this example) and replace using a non-black color like blue. Search and replace delimiter (in this case quote marks) color from blue to black. Perform changes to content in blue. Select all and change to black. If this comes up often, I would suggest macros on a toolbar for the step one (blue, take blue off delimiter) and step two (change all to black).

Rhonda
  • 1
  • 1
0

You are wrong. Word does support some wildcards, ? for a single character and * for a series of characters.

This is not a regular expression

means no lookbehind and no lookahead

stema
  • 90,351
  • 20
  • 107
  • 135
  • Ok, sorry I called it 'regular expression'. I know that Word supports wildcards (hence I used `"*"`). That leaves the question though: is it possible to achieve what I want with MS Word? – Daan Mar 14 '12 at 11:57
0

While there will never be everything in Ms-Word that you want, e.g. like this one where you want to find something else, but want to select only a part of it, there are always macros which you can program to accomplish your task.

Add the following VBA code to your document. You can add a custom button on the toolbar to call it.

Sub FindSpecial()
    FindSpecialA
End Sub

Private Sub FindSpecialA(Optional text As String)
    Dim ToFind As String

    ToFind = InputBox("Enter the text you want to find in double-quotes (without double-quotes):" & vbCrLf & vbCrLf & "(Enter * to match anything within double-quotes)", "Find", text)
    If ToFind = "" Then Exit Sub

    Selection.Find.ClearFormatting
    With Selection.Find
        .text = """" & ToFind & """"
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        If ToFind = "*" Then
            .text = "[“""]*[”""]"
            .MatchWildcards = True
        End If
    End With
    Selection.Find.Execute
    If Selection.Find.Found Then
        Selection.MoveStart unit:=wdCharacter, Count:=1
        Selection.MoveEnd unit:=wdCharacter, Count:=-1
        FindSpecialA ToFind
    Else
        MsgBox "Not found!"
    End If
End Sub

EDIT:

Updated the code to handle wildcard * matches.

Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
0

Some versions of MS Word support regex-style groups with their "search with wildcards" option, meaning that if you can create a search expression between two quotes -- the one that works for me is "?@" -- you can change it to "(?@)" and enter \1 for the replace text. This will replace the text that was found with just the text that matches the expression between the parentheses, getting rid of your quote marks. (MS Word's ?@ is equivalent to .* (non-greedy) in common regex.)

This works for me in Word 2008 for Mac, but I don't have a guide to which versions of Office support this syntax.

Beware! In this search form, Word does not equate the straight quotes on your keyboard with the curly quotes it inserts in order to look pretty. You will need to either turn off "smart quotes" for this document, or construct your search phrase by cutting and pasting the opening and closing quote characters from your document.

octern
  • 4,825
  • 21
  • 38
  • I think it works exactly as you describe. However, in the end it only removes the quotes. It doesn't filter out any other unwanted content. I want to select/extract _only_ the contents inside the quotes. – Daan Mar 15 '12 at 15:15
  • What happens if you try to search for and delete all text between a closing quote mark and an opening quote mark? – octern Mar 15 '12 at 16:38