Check before leaving the paragraph

E

Eric

All of our documents have names like 'A123 Procedure to do
lots of things.doc'. When we click in the name and run the
macro various processing takes place.

The macro does a find 'backward' and looks for 'A123' and
book marks it. It then finds 'forward' for '.doc'. A range
is then defined and the name is selected.

What we want to check is that we don't leave the paragraph
with the file name in it. For instance if the 'A123' was
missing off the file name, the search backwards would keep
going until it hits 'A123' in a filename in a previous
paragraph.

Some ideas we had for checking was:
- search for the paragraph marks (before and after the
paragraph with the file name)then create a range between
for searching forwards/backwards.
- do the search forwards/backwards, get the filename and
do a check using InStr to see if a paragraph mark was
captured.

Are there other simpler ways of doing this?
 
J

Jonathan West

Hi Eric,

It would seem that the simplest approach is to restrict the search both
forward and backward to the current paragraph. This is not too hard and is
an excellent example of the power you can get from using Range objects. Your
block of Find text would look something like this

Dim myFindStart as Range
Set myFindStart = Selection.Paragraphs(1).Range
myFindStart.End = Selection.End
With myFindStart.Range
.Text = "A123"
.Forward = False ' searching backwards
.Wrap = wdFindStop ' don't look outside the para
If .Execute
' you have found the text, and myFindStart
' is marking the location of A123
Else
' A123 not found
End If
End With

' now look for the end
Dim myFindEnd as Range
Set myFindEnd = Selection.Paragraphs(1).Range
myFind.Start = Selection.Start
With myFind.Range
.Text = ".doc"
.Forward = True ' searching forwards tis time
.Wrap = wdFindStop ' don't look outside the para
If .Execute
' you have found the text, and myFindEnd
' is marking the location of .doc
Else
' .doc not found
End If
End With

'next bit assumes that both .doc and A123 were found
ActiveDocument.Bookmarks.Add Name:="MyBookmark", _
Range:=ActiveDocument.Range(myFindStart.Start, myFindEnd.End)

All this is done without moving the selection at all.
 
H

Helmut Weber

Hi Eric,
All of our documents have names like
'A123 Procedure to do lots of things.doc'.
When we click in the name and run the
macro various processing takes place.

Ho do you click in the name of the document?
Could it be you ar talking of a heading or
a paragraph that should contain the docs name?

In principle, if I get you right, I would
define a range that consists of the paragraph
the cursor is in, like
Dim oRng As Range
Set oRng = Selection.Paragraphs(1).Range

and restrict the search to that range.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
E

Eric

Jonathan

Thanks for this. There were a couple of typos but now
works fine. The code as I altered it follows.
Unfortunately I don't quite understand the logic. For
instance why set myFindStart.End = Selection.End when the
search is actually backwards.

Thanks again
Eric


Dim myFindEnd As Range
Dim myFindStart As Range

Set myFindStart = Selection.Paragraphs(1).Range
myFindStart.End = Selection.End
With myFindStart.Find
.Text = "http"
.Forward = False ' searching backwards
.Wrap = wdFindStop ' don't look outside the para
If .Execute Then
' found
Else
' not found
End If
End With

Set myFindEnd = Selection.Paragraphs(1).Range
myFindEnd.Start = Selection.Start
With myFindEnd.Find
.Text = ".doc"
.Forward = True ' searching forwards tis time
.Wrap = wdFindStop ' don't look outside the para
If .Execute Then
' found
Else
' not found
End If
End With

'next bit assumes that both .doc and A123 were found
ActiveDocument.Bookmarks.Add Name:="MyBookmark", _
Range:=ActiveDocument.Range(myFindStart.Start,
myFindEnd.End)
 

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