[RESOLVED] Help with returning an object
Hi guys, thanks for taking the time to look at this problem. I am writing a word macro and am finding it difficult to return a Range object from a function. Here is the code (i have commented out the unneccessary code):
VB Code:
Sub start()
Dim aRange As Range
aRange = getTestNameAndNumber(ActiveDocument)
'MsgBox testNumber
' MsgBox testName
End Sub
Public Function getTestNameAndNumber(ByRef aDocument As Document) As Range
Dim aRange As Word.Range
Set aRange = aDocument.TablesOfContents(1).Range
' Dim i As Integer
' i = 1
' Do
'If (CInt(Mid(aRange.Sentences.Item(i), 1, 1) = 3)) Then 'Finds the first 3 in contents
' Exit Do
' End If
' i = i + 1
' Loop While i < aRange.Sentences.Count
getTestNameAndNumber = aRange
'aRange.Sentences.Item(i)
End Function
It gives the following error "Object variable or With block variable not set (Error 91)"
Thanks for your time:)
<>GT<>
Re: Help with returning an object
To return an object you must use the Set keyword.
VB Code:
Set getTestNameAndNumber = aRange
Re: Help with returning an object
Try:
VB Code:
Set aRange = getTestNameAndNumber(ActiveDocument)
Re: Help with returning an object
Perfect, thanks guys for your quick response:)
Im still learning VB:)
Re: Help with returning an object
When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.