Search for a word within first 10 lines of a document

D

Dusty

Hi, is there a way I can search for an occurance of a word within the first
10 lines only of a document?

I want a message box to say "Yes" if found, or "No" if not. I actually want
to do more with the macro, but this just keeps things simple for now.

Thanks.
 
S

StevenM

To: Dusty,

Take a look at the following:

'
' Test FindInLine
'
Sub TestFindInLine()
Dim newDoc As Document
Dim oRange As Range
Dim linesRange As Range
Dim sDummyText As String
Dim sFind(1 To 3) As String
Dim nIndex As Long

Set newDoc = Documents.Add
Set oRange = newDoc.Range

sFind(1) = "no less than 65 properties"
sFind(2) = "in addition"
sFind(3) = "contguous"
'The next line is very long
sDummyText = "The range object represents a contiguous area within a
Word document. But in addition, the range object has no less than 65
properties, only one of which is the text property. Although a range can work
with any part of a document, it is more difficult to work with pages or lines
than it is to work with paragraphs, sentences, or words. The reason for this
is that while VBA has collections for sections, paragraphs, sentences, words,
and characters, there are no collections for pages and lines. My guess would
be that a page doesn't fit into a logical hierarchy, since sections,
paragraphs, sentences, and even words, break across pages and lines.
Nonetheless, it is possible to work with pages and lines."

oRange.Text = sDummyText & vbCr & sDummyText
Set linesRange = FindRangeOfLines(oRange, 10)
linesRange.Select
MsgBox "10 Lines"
For nIndex = 1 To 3
Set oRange = FindInRange(linesRange, sFind(nIndex))
If Not oRange Is Nothing Then
oRange.Select
MsgBox "Found: " & oRange.Text
oRange.Collapse wdCollapseEnd
oRange.Select
Else
MsgBox "Did not find: " & sFind(nIndex)
End If
Next nIndex
linesRange.Select
MsgBox "10 Lines"
End Sub
'
' FindRangeOfLines a number of lines within a range
' I couldn't get "ByVal" to work with ranges
' so I'm adding: Set lnRange = oRange.Duplicate
'
Function FindRangeOfLines(oRange As Range, ByVal nNumberOfLines) As Range
Dim nEnd As Long
Dim nStart As Long
Dim nLnEnd As Long
Dim nCount As Long
Dim lnRange As Range

Set lnRange = oRange.Duplicate
nEnd = oRange.End
nStart = oRange.Start
lnRange.Collapse wdCollapseStart
lnRange.Select
Set lnRange = oRange.Parent.Bookmarks("\line").Range
lnRange.Move wdCharacter, 1
nLnEnd = lnRange.End
While (nLnEnd < nEnd And nCount < nNumberOfLines)
nCount = nCount + 1
lnRange.Select
Set lnRange = oRange.Parent.Bookmarks("\line").Range
lnRange.Move wdCharacter, 1
nLnEnd = lnRange.End
Wend
lnRange.Start = nStart
Set FindRangeOfLines = lnRange
End Function

'
' FindInRange finds a string within a range
' I couldn't get "ByVal" to work with ranges
' so I'm adding: Set fRange = findRange.Duplicate
'
Function FindInRange(findRange As Range, ByVal sStr As String) As Range
Dim fRange As Range
Set fRange = findRange.Duplicate
Dim nPos As Long
nPos = InStr(fRange.Text, sStr)
If nPos > 0 Then
fRange.Start = fRange.Start + nPos - 1
fRange.End = fRange.Start + Len(sStr)
Set FindInRange = fRange
Else
Set FindInRange = Nothing
End If
End Function

Steven Craig Miller
 
H

Helmut Weber

Hi Dusty,

like that:

Sub Test903a()
Dim l As Long
Dim rTmp As Range
Set rTmp = Selection.Range
Selection.HomeKey unit:=wdStory
rTmp.start = Selection.start
For l = 1 To 9
Selection.MoveDown
Next
rTmp.End = Selection.Bookmarks("\line").Range.End
' rTmp.Select ' for testing
With rTmp.Find
.Text = "JOHN BROWN"
If .Execute Then
MsgBox "found"
Else
MsgBox "not found"
End If
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Gregory K. Maxey

Helmut,

Or like this:

Sub ScratchMacro()
Dim objPage As Page
Dim MyRect As Rectangle
Dim oRng As Range
Set objPage = ActiveDocument.ActiveWindow.Panes(1).Pages(1)
Set MyRect = objPage.Rectangles(1)
Set oRng = MyRect.Range
oRng.End = MyRect.Lines(9).Range.End
With oRng.Find
.Text = "Helmut Weber"
If .Execute Then
MsgBox "found"
Else
MsgBox "not found"
End If
End With
End Sub
 
H

Helmut Weber

Hi Greg,

help displays nothing but a blank screen,
on "page" as well as on "rectangle".

Sure, your solution is working.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Gregory K. Maxey

Helmut,

What version of Word are you working with? I think these objects where
added in Word2003. Works in both Word 2003 and Word 2007.
 
H

Helmut Weber

Gregory,

I'm using Office 2003 (XP?).
It's just that help on "page" and "rectangle "doesn't work.

Have a nice day.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
T

Tony Jollans

It would be really nice if this did work, but ..

The rectangles collection contains a complete page layout, top to bottom,
left to right, starting with the page header and including elements in the
drawing layer (watermarks, perhaps) so, at the very least, you need to do
some more checking on what the rectangle is before you can start using its
range like that.
 
G

Gregory K. Maxey

All true. Thanks Tony.


Tony said:
It would be really nice if this did work, but ..

The rectangles collection contains a complete page layout, top to
bottom, left to right, starting with the page header and including
elements in the drawing layer (watermarks, perhaps) so, at the very
least, you need to do some more checking on what the rectangle is
before you can start using its range like that.
 

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