Set Range using Find

A

Andrew

This seems simple but I'm struggling since I've always used selections rather
than ranges. I want to set multiple ranges based on search criteria.

For example, the macro will search for QUESTION 1.1. Then I want to set the
range to be the pargraphs between QUESTION 1.1 and RESPONSE TO QUESTION 1.1.
(Note I want to capture the question, not the response.)

Thanks!
 
H

Helmut Weber

Hi Andrew,
kind of hard to explain all that is to it
here in a few lines. I set up an array of
ranges and redim it for each found question.
Each range in the array (except(0)) gets assigned
the result of find minus 17 letters, the length
of "RESPONSE QUESTION", which is in included in
the result of "find".
Just one of many ways, leaving lots of possibilities
for improvement.
---
Sub Test712()
Dim rFnd As Range
Dim rTmp() As Range
Set rFnd = ActiveDocument.Range
Dim i As Integer
Resetsearch
With rFnd.Find
.Text = "QUESTION*RESPONSE QUESTION"
.MatchWildcards = True
.MatchCase = True
While .Execute
i = i + 1
ReDim Preserve rTmp(i)
Set rTmp(i) = rFnd.Duplicate
rTmp(i).End = rTmp(i).End - 17
Wend
End With
Resetsearch
For i = 1 To UBound(rTmp)
MsgBox rTmp(i)
Next
End Sub
'---
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
.Execute
End With
End Sub
 

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