Do while the current section is a specific section of the doc

H

hr38581

I am creating an MS Word macro that should search the first section of the
document for specific text, copy all text in the paragraph (that contained
the found text), and paste the text in a table in a new document. (Repeat
for section 2 but with different search criteria).
I am creating arrays to store all the found text. At the end of the macro I
create a new doc with a new table and enter all the data from the arrays.
My problem is with limiting each search to its appropriate section. (I
don't want my Section 1 search to return text found in Section 2 (or other
sections) of the doc.)
Here is a portion of my code (there are additional loops within my search
that are not included here):

Selection.HomeKey Unit:=wdStory
Do While ActiveDocument.Bookmarks("\Section") = ActiveDocument.Sections(1)
With Selection.Find
.ClearFormatting
.Forward = True
Do While .Execute(FindText:="Test Name : ") = True
Selection.Bookmarks("\Para").Select
a = a + 1
ReDim Preserve PrePlanName(a)
Set PrePlanName(a) =
ActiveDocument.Range(Start:=Selection.Start, _
End:=Selection.End)
Selection.Collapse Direction:=wdCollapseEnd
Loop
End With
Loop

I get a type mismatch on my comparison between
'ActiveDocument.Bookmarks("\Section")' and 'ActiveDocument.Sections(1)'. So
how do I compare "current section" with "Section 1 of the doc"?
 
H

Helmut Weber

Hi,

much to complicated.
Have a close look at that one:

Sub Mytest()
Dim a As Long
Dim rSct As Range
Dim PrePlanName() As String
Set rSct = ActiveDocument.Sections(1).Range
With rSct.Find
.Text = "Test Name : "
While .Execute
a = a + 1
ReDim Preserve PrePlanName(a)
PrePlanName(a) = rSct.Paragraphs(1).Range.Text
Wend
End With
' MsgBox PrePlanName(2)
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

hr38581

I think I spoke too soon, Helmut.
The search was much simpler and returned the desired data from Section 1 but
it also returned data from Section 2. What went wrong?
 
H

Helmut Weber

I think I spoke too soon, Helmut.
So did I.

Maybe this helps:

Sub Mytest()
Dim a As Long
Dim rSct As Range
Dim PrePlanName() As String
Set rSct = ActiveDocument.Sections(1).Range
With rSct.Find
.Text = "Test Name : "
While .Execute
a = a + 1
ReDim Preserve PrePlanName(a)
rSct.Select
If rSct.start + 1 > ActiveDocument.Sections(1).Range.End Then '!
Exit Sub
End If
PrePlanName(a) = rSct.Paragraphs(1).Range.Text
Wend
End With
' MsgBox PrePlanName(2)
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

Another way to do the same thing is to use the range object's .InRange method to
determine when the search range has gone outside the desired section:

Sub Mytest2()
Dim a As Long
Dim rSct As Range
Dim PrePlanName() As String
Set rSct = ActiveDocument.Sections(1).Range
With rSct.Find
.Text = "Test Name : "
While .Execute And _
rSct.InRange(ActiveDocument.Sections(1).Range)
a = a + 1
ReDim Preserve PrePlanName(a)
PrePlanName(a) = rSct.Paragraphs(1).Range.Text
Wend
End With
' MsgBox PrePlanName(2)
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
H

hr38581

Thank you very much, Jay. Using the object's .InRange method simplified my
code even more. I've learn a great deal from Helmut and you!
 

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