Word VBA - Find text between delimiters and convert to lower case

Posted by jJack on Stack Overflow See other posts from Stack Overflow or by jJack
Published on 2009-10-03T00:31:56Z Indexed on 2012/06/10 4:40 UTC
Read the original article Hit count: 322

Filed under:
|
|
|

I would like to find text which is between the < and > characters, and then turn any found text into "normal" case, where first letter of word is capitalized. Here is what I have thus far:

Function findTextBetweenCarots() As String

Dim strText As String

With Selection
    .Find.Text = "<"  ' what about <[^0-9]+>  ?
    .Find.Forward = True
    .Find.Wrap = wdFindContinue
End With

Selection.Find.Execute


 ' Application.Selection. ' how do I get the text between the other ">"?
    findCarotSymb = Application.Selection.Text

End Function

Or, is there a better way of doing this? I also approached the problem using the VBScript Regex 5.5 library, which worked on simple documents, but not on certain documents with complex tables. For example, trying to just bold the text (for simplicity):

 Sub BoldUpperCaseWords()

  Dim regEx, Match, Matches
  Dim rngRange As Range

  Set regEx = New RegExp
  regEx.Pattern = "<[^0-9]+>"
  regEx.IgnoreCase = False
  regEx.Global = True

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

  For Each Match In Matches

     ActiveDocument.Range(Match.FirstIndex, Match.FirstIndex + Len(Match.Value)).Bold = True

  Next

End Sub

would not work in a document with tables. In fact, it would not even bold the correct text (the text between the <>. This leads me to believe I have a broader issue here that I am missing.

Here is what a sample doc looks like. Notice the wrong text is bold:

alt text

© Stack Overflow or respective owner

Related posts about regex

Related posts about vba