Can one pass a Range object to a function?

E

Emily

Hello,

I have a subroutine that sets a Range in a wORD97 document. I would
like to examine the contents (text) of the Range by using it as a
variable passed to a function, which function will examine the Range
for a keyword, and return a "Yes" to the subroutine that called it,
if the keyword was found.

Can I pass a Range object to a function as a variable?

If not, how else would I accomplish this?

TIA
 
J

Jay Freedman

Emily said:
Hello,

I have a subroutine that sets a Range in a wORD97 document. I would
like to examine the contents (text) of the Range by using it as a
variable passed to a function, which function will examine the Range
for a keyword, and return a "Yes" to the subroutine that called it,
if the keyword was found.

Can I pass a Range object to a function as a variable?

If not, how else would I accomplish this?

TIA

Hi Emily,

Yes, you certainly can pass a Range object as a parameter. Try this:

Public Sub Calling_Procedure()
Dim MyRange As Range
Set MyRange = Selection.Range
If Called_Function(MyRange) Then
MsgBox "The selection contains 'the'"
Else
MsgBox "The selection does not contain 'the'"
End If
Set MyRange = Nothing
End Sub

Private Function Called_Function(CheckRange As Range) As Boolean
' return True if CheckRange.Text
' contains 'the' (case-insensitive)
If InStr(LCase(CheckRange.Text), "the") > 0 Then
Called_Function = True
Else
Called_Function = False
End If
End Function
 
J

Jean-Guy Marcil

Emily was telling us:
Emily nous racontait que :
Hello,

I have a subroutine that sets a Range in a wORD97 document. I would
like to examine the contents (text) of the Range by using it as a
variable passed to a function, which function will examine the Range
for a keyword, and return a "Yes" to the subroutine that called it,
if the keyword was found.

Can I pass a Range object to a function as a variable?

Yes, but it seems that all you need to pass is the text content of the
range.
For example, looking for the word "quick" in the user selected range:

'_______________________________________
Sub Test()

Dim myRange As Range
Set myRange = Selection.Range

If HasText(myRange.Text) Then
MsgBox "Text found"
Else
MsgBox "Text NOT found"
End If

End Sub
'_______________________________________
'_______________________________________
Function HasText(checkText As String) As Boolean

HasText = False

If InStr(1, checkText, "quick") > 0 Then HasText = True

End Function
'_______________________________________

But if you insit on passing the range, do it like this:

'_______________________________________
Sub Test()

Dim myRange As Range
Set myRange = Selection.Range

If HasText(myRange) Then
MsgBox "Text found"
Else
MsgBox "Text NOT found"
End If

End Sub
'_______________________________________
'_______________________________________
Function HasText(checkRange As Range) As Boolean

HasText = False

If InStr(1, checkRange.Text, "quick") > 0 Then HasText = True

End Function
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Emily

Thank you very much for your help Jay and Jean-Guy. I did do Google
and MSDN searches on this, but couldn't find it exactly, and my
initial attempts at coding had some problems that you both helped me
to solve.

Thanks again ...

Em
 

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