+ Reply to Thread
Results 1 to 6 of 6

"Object required" error upon "Exit Function" statement

Hybrid View

6StringJazzer "Object required" error upon... 12-24-2018, 01:30 PM
shg Re: "Object required" error... 12-24-2018, 03:31 PM
shg Re: "Object required" error... 12-24-2018, 03:35 PM
mjr veverka Re: "Object required" error... 12-24-2018, 04:13 PM
shg Re: "Object required" error... 12-24-2018, 04:54 PM
6StringJazzer Re: "Object required" error... 12-24-2018, 06:40 PM
  1. #1
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,800

    "Object required" error upon "Exit Function" statement

    I have an OpenProject function in Excel VBA that returns an Object. The purpose is to open a Microsoft Project file and return the Project as an object. The code is being written to be able to run in either Excel or Project. The OpenProject function opens the project correctly and correctly assigns it to the return value for the function. When I debug after the project is opened, I am able to use commands in the Immediate window to get the name of the project and number of tasks, so I know the object is valid.

    However, when the Exit Function statement is executed I get an "Object required" error. I have seen this error occur on various statements but never on an Exit Function statement when the return object is valid. What could possibly cause this on Exit Function?

    I have attached a file that has been skinnied down to the minimal code to reproduce the error.
    Attached Files Attached Files
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: "Object required" error upon "Exit Function" statement

    I'm surprised that compiles; the invocation of OpenProject doesn't agree with OpenProject's signature.

    Set SourceProject = OpenProject(ExcelExport_Apps.MSP)
    ...
    Public Function OpenProject() As Object
    Fix that and it works:

    Set SourceProject = OpenProject()
    Last edited by shg; 12-24-2018 at 03:34 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: "Object required" error upon "Exit Function" statement

    Or early-bound,

    Public appXL        As Excel.Application
    Public appPr        As MSProject.Application
    
    Public Sub Test()
      Dim SourceProject As Object  ' Project ' The Microsoft Project object we are going to export
    
      ' Establish applications
      Select Case Application.Name
        Case "Microsoft Excel"
          Set appXL = Application
          Set appPr = New MSProject.Application
          ' Ask the user for the project
          Set SourceProject = OpenProject()
    
        Case "Microsoft Project"
          Set appXL = New Excel.Application
          appXL.Visible = True
          Set appPr = Application
          ' Use the active project
          Set SourceProject = appPr.ActiveProject
      End Select
    
    End Sub
    
    Public Function OpenProject() As Object  ' Project
      ' Open Project and prompt the user for a Project file to open
      ' Return value is the Project object
      ' Project is left open, minimized
    
      appPr.FileOpen
    
      On Error GoTo NoProject
      Set OpenProject = appPr.ActiveProject
      Exit Function
    
    NoProject:
    
      appPr.FileExit
    
      If Err.Number = 424 Then
        ' File not found
        MsgBox "No Project selected, exiting."
    
      Else
        MsgBox "Unexpected error attempting to open Project file" & vbLf & _
               "Exiting." & vbLf & _
               Err.Number & " " & Err.Description
      End If
    End Function

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,254

    Re: "Object required" error upon "Exit Function" statement

    1. What if 'MSProject' is not installed ?

    Maybe:
                'Set ExcelExport_Apps.MSP = CreateObject("MSProject.Application")
                On Error Resume Next
                    Set ExcelExport_Apps.MSP = CreateObject("MSProject.Application")
                    If ExcelExport_Apps.MSP Is Nothing Then MsgBox "MSProject not installed": Exit Sub
                On Error GoTo 0
    2. ... in function:
    '...
    Exit Function
    
    NoProject:
        '...
    Exit Function 'unnecessary
    End Function

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: "Object required" error upon "Exit Function" statement

    Exit Function 'unnecessary
    I believe it is necessary, otherwise the Else clause will execute.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,800

    Re: "Object required" error upon "Exit Function" statement

    Quote Originally Posted by shg View Post
    I'm surprised that compiles; the invocation of OpenProject doesn't agree with OpenProject's signature.
    Thanks for finding that! I am surprised it compiles, too. I am also surprised that it fails on the Exit Function statement, of all places. But your fix worked.

    Quote Originally Posted by shg View Post
    Exit Function 'unnecessary
    I believe it is necessary, otherwise the Else clause will execute.
    On review I think that final Exit Function is indeed unnecessary since it is the last line of code in the function. I think it must be an artifact leftover from when it was followed by some other code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] "ActiveWorkbook.Path" fails - triggers "Compile Error Object required"
    By Ochimus in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-15-2018, 02:58 PM
  2. Replies: 35
    Last Post: 01-13-2016, 02:16 AM
  3. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  4. "The "sheets" method from the "_Global" object have failed."
    By mankit87 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2011, 08:53 AM
  5. Error msgs: "Object varible or with block variable not set"; "subscript out of range"
    By menyanthe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-26-2009, 04:58 PM
  6. Replies: 3
    Last Post: 12-14-2006, 01:36 PM
  7. What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1