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:
  1. Sub start()
  2. Dim aRange As Range
  3.  
  4. aRange = getTestNameAndNumber(ActiveDocument)
  5.  
  6.  'MsgBox testNumber
  7. ' MsgBox testName
  8. End Sub
  9.  
  10. Public Function getTestNameAndNumber(ByRef aDocument As Document) As Range
  11.    
  12.     Dim aRange As Word.Range
  13.     Set aRange = aDocument.TablesOfContents(1).Range
  14.    
  15.    ' Dim i As Integer
  16.    ' i = 1
  17.    ' Do
  18.         'If (CInt(Mid(aRange.Sentences.Item(i), 1, 1) = 3)) Then 'Finds the first 3 in contents
  19.          '  Exit Do
  20.        ' End If
  21.       '  i = i + 1
  22.    ' Loop While i < aRange.Sentences.Count
  23.    
  24. getTestNameAndNumber = aRange
  25. 'aRange.Sentences.Item(i)
  26.  
  27. End Function

It gives the following error "Object variable or With block variable not set (Error 91)"

Thanks for your time
<>GT<>