VBA and regular expression to search for ranges of text

E

enda_ridge

Hi,
I'd be very grateful for help with the following.

I have a text file open in Word. I need an example of how to use a
regular expression to search for ranges of text that begin with
'\begin{figure}' and end with '\end{figure}' inclusive.
\begin{figure}
Any text can go here etc etc etc
\end{figure}

Once this is found, I would like to convert it to a Word Range object
and then continue the regular expression search from the next point in
the document.

Thanks in advance,

Enda
 
H

Helmut Weber

Hi Enda,

lke this:


Sub Test7884()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
ResetSearch
With rTmp.Find
.Text = "\\begin\{figure\}*\\end\{figure\}"
.MatchWildcards = True
While .Execute
rTmp.Select ' for testing
Wend
ResetSearch
End With
End Sub
' ----
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

You don't have to convert what you found to a range.
It is a range already. The question is, what do want to do with it.


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top