Results 1 to 5 of 5

Thread: [RESOLVED] Help with returning an object

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Resolved [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:
    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<>

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Help with returning an object

    To return an object you must use the Set keyword.
    VB Code:
    1. Set getTestNameAndNumber = aRange

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Help with returning an object

    Try:
    VB Code:
    1. Set aRange = getTestNameAndNumber(ActiveDocument)
    CS

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Help with returning an object

    Perfect, thanks guys for your quick response

    Im still learning VB

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    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.
    CS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width