Hi Bear
Sure. Here's a little example which shows using a range as a parameter to a
Sub and a Function.
Option Explicit
Sub StartHere()
Dim rngSelection As Word.Range
Dim sStyleName As String
'Get a reference to the selected range
'(in real life you need error checking here in
'case the user has selected, eg, an image)
Set rngSelection = Selection.Range
'Send our selection off to the mGetRangeStyle function
'to be processed, and get back the name of the style
sStyleName = mGetFirstParaStyle(oRange:=rngSelection)
'Use the return value to tell the user what's happening
MsgBox "The first paragraph in the selection is in style " & sStyleName
'Send our range off to the mMakeRangePink sub that
'will format the font of our range as pink
mMakeRangePink oRange:=rngSelection
End Sub
Private Function mGetFirstParaStyle(ByRef oRange As Word.Range) As String
Dim sRetval As String
'Make sure we have a range to play with..
If Not oRange Is Nothing Then
'Get the name of the style of the first
'paragraph of our range
'(in real life you need error checking here in case
'we're in a protected document or something'
sRetval = oRange.Paragraphs(1).Style
End If
'Set the return value
mGetFirstParaStyle = sRetval
End Function
Private Sub mMakeRangePink(ByRef oRange As Word.Range)
'Make sure we have a range to play with
If Not oRange Is Nothing Then
'make it pink
oRange.Font.Color = wdColorRose
End If
'This is a Sub so it doesn't have a return value
'But it has actually *changed* the oRange.
End Sub
I explicitly put in "ByRef" in the arguments of the Sub and Function to
remind us that an object is, by default, passed "By reference". That means
that the lower-level routine receives a reference to the object, and
anything you do to the object (eg, make it pink) actually changes the
object.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
Bear said:
Shauna:
That's something like what I ended up doing. But I was trying to pass a
range to a function, and couldn't make it work. In the end I defined a
module-level range object and had the subroutine work on that.
Can one pass a range to a function or subroutine in other ways?
Bear