Defining a specific range

S

supersub15

Hi there,

I'm trying to create a macro that opens Word documents in a folder and
searches for the words "Changes due to". However, I want the search to
take place in a range that starts at the "Overview" word (in a Heading
2 style) and ends when the next Heading 2 is met.

I've done some searches on this forum, but I can't find this specific
definition of my range.

Thanks for any help.
Carlos
 
H

Helmut Weber

Hi Carlos,

like that:

Sub Test7a()
Dim rTmp1 As Range
Dim rTmp2 As Range
Dim x1 As Long
Dim x2 As Long
Set rTmp1 = ActiveDocument.Range
Set rTmp2 = Selection.Range
With rTmp1.Find
.Text = "overview"
.Style = "Heading 2"
If .Execute Then
x1 = rTmp1.start
rTmp2.start = rTmp1.Paragraphs(1).Next.Range.start
rTmp2.End = ActiveDocument.Range.End
End If
End With
With rTmp2.Find
.Style = "Heading 2"
If .Execute Then
x2 = rTmp2.Paragraphs(1).Previous.Range.End
End If
End With
rTmp2.start = x1
rTmp2.End = x2
rTmp2.Select ' for testing
With rTmp2.Find
.Text = "Changes due to"
While .Execute
' your code
Wend
End With
End Sub

Though the part of "' your code" might be quite difficult.



--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

To all the co-readers,
there was a mail contact in between,
specifying that there may be more strings like
"Changes due to FS[0-9]{5}" in the range,
and "FS[0-9]{5}" should be stored in a new doc.

Hi Carlos,

the question is, what pattern follows the expression
you are looking for. I'm assuming, it is
"Changes due to " followed by "FS" followed by 5 digits.

If so:

Sub Test7a()
Dim rTmp1 As Range
Dim rTmp2 As Range
Dim nDoc As Document
Dim x1 As Long
Dim x2 As Long
Set rTmp1 = ActiveDocument.Range
Set rTmp2 = Selection.Range
With rTmp1.Find
.Text = "overview"
.Style = "Heading 2"
If .Execute Then
x1 = rTmp1.start
rTmp2.start = rTmp1.Paragraphs(1).Next.Range.start
rTmp2.End = ActiveDocument.Range.End
End If
End With
With rTmp2.Find
.Style = "Heading 2"
If .Execute Then
x2 = rTmp2.Paragraphs(1).Previous.Range.End
End If
End With
rTmp2.start = x1
rTmp2.End = x2
rTmp2.Select ' for testing
Set nDoc = Documents.Add
With rTmp2.Find
.ClearFormatting
.Text = "Changes due to FS[0-9]{5}"
.MatchWildcards = True
While .Execute
MsgBox Right(rTmp2, 7) ' for testing
nDoc.Range.InsertAfter vbCrLf & Right(rTmp2, 7)
' your code
Wend
End With
nDoc.Paragraphs(1).Range.Delete
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
S

supersub15

To all the co-readers,
there was a mail contact in between,
specifying that there may be more strings like
"Changes due to FS[0-9]{5}" in the range,
and "FS[0-9]{5}" should be stored in a new doc.

Hi Carlos,

the question is, what pattern follows the expression
you are looking for. I'm assuming, it is
"Changes due to " followed by "FS" followed by 5 digits.

If so:

Sub Test7a()
Dim rTmp1 As Range
Dim rTmp2 As Range
Dim nDoc As Document
Dim x1 As Long
Dim x2 As Long
Set rTmp1 = ActiveDocument.Range
Set rTmp2 = Selection.Range
With rTmp1.Find
   .Text = "overview"
   .Style = "Heading 2"
   If .Execute Then
      x1 = rTmp1.start
      rTmp2.start = rTmp1.Paragraphs(1).Next.Range.start
      rTmp2.End = ActiveDocument.Range.End
   End If
End With
With rTmp2.Find
   .Style = "Heading 2"
   If .Execute Then
      x2 = rTmp2.Paragraphs(1).Previous.Range.End
   End If
End With
rTmp2.start = x1
rTmp2.End = x2
rTmp2.Select ' for testing
Set nDoc = Documents.Add
With rTmp2.Find
   .ClearFormatting
   .Text = "Changes due to FS[0-9]{5}"
   .MatchWildcards = True
   While .Execute
      MsgBox Right(rTmp2, 7) ' for testing
      nDoc.Range.InsertAfter vbCrLf & Right(rTmp2, 7)
      ' your code
   Wend
End With
nDoc.Paragraphs(1).Range.Delete
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Thanks Helmut. I will give it a try and report back.
 

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