Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

Can one pass a Range object to a function?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Emily - 11 Jul 2005 16:07 GMT
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
Jay Freedman - 11 Jul 2005 16:18 GMT
> Hello,
>
[quoted text clipped - 9 lines]
>
> 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

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

Jean-Guy Marcil - 11 Jul 2005 16:19 GMT
Emily was telling us:
Emily nous racontait que :

> Hello,
>
[quoted text clipped - 5 lines]
>
> 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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Emily - 11 Jul 2005 17:16 GMT
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

>Hello,
>
[quoted text clipped - 9 lines]
>
>TIA
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.