Results 1 to 9 of 9

Thread: Object invoke error ... sometimes

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Object invoke error ... sometimes

    Hello all,

    I'm experiencing a rather frustrating error in both Excel VBA 2003/2007. It only happens sometimes (somewhat rarely), during the below code. I have 5 necessary references, which I can list if need be. Having read the MS help ( http://support.microsoft.com/default...;en-us;Q319832 ) on this topic I feel I may need to "fully qualify" my object references in the below code, but don't really know how to go about doing that...

    Here's the code w/ the problem. The below code essentially opens and copies a worksheet from a workbook into the active workbook, then closes the workbook.

    vb Code:
    1. Application.DisplayAlerts = False
    2. Dim sfilename As String
    3. sfilename = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", Title:="N-INDEX")
    4. If sfilename = "False" Then Exit Sub
    5. Dim temp As String
    6. temp = spliceFileNameEnd2(sfilename)
    7. Dim shtnext As Variant
    8. For Each shtnext In Sheets
    9.     If shtnext.name = "pindexCA" Then 'Search/Delete charts w/ same name
    10.         Sheets("pindexCA").Visible = True
    11.         Application.DisplayAlerts = False 'No delete prompt
    12.         Sheets("pindexCA").Delete
    13.     End If
    14. Next shtnext
    15. Workbooks.Open FileName:=sfilename
    16. Dim found As Boolean
    17. found = False
    18. For Each shtnext In Sheets
    19.     If shtnext.name = "pindexCA" Then
    20.         found = True
    21.         Exit For
    22.     End If
    23. Next shtnext
    24. If found = False Then
    25.     Workbooks(temp).Close SaveChanges:=False
    26.     MsgBox ("No P-Index File was found in that file.")
    27.     Sheets.Add
    28.     ActiveSheet.name = "pindexCA"
    29.     Sheets("pindexCA").Visible = xlVeryHidden
    30.     Exit Sub
    31. End If
    32. Sheets("pindexCA").Copy After:=Workbooks(ThisWorkbook.name).Sheets(ThisWorkbook.Sheets.count)
    33. Workbooks(temp).Close SaveChanges:=False
    34. Sheets("pindexCA").Visible = xlVeryHidden
    35. Sheets("Driver").Activate
    36. Application.DisplayAlerts = True

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Object invoke error ... sometimes

    Workbooks.Open FileName:=sfilename
    i would set the open workbook to an object
    set objwb = Workbooks.Open FileName:=sfilename

    you can then use the object to know you are working with the correct workbook


    vb Code:
    1. For Each shtnext In objwb.Sheets
    2.     If shtnext.name = "pindexCA" Then
    3.         found = True
    4.         Exit For
    5.     End If
    6. Next shtnext
    if the loop completes shtnext will be empty, but if the sheet is found and exit for is executed shtnext will still contain the worksheet object and you can work with that
    if shtnext is declared as a worksheet, you can test if it is nothing instead of using a boolean to know if it was found

    same when you add a sheet, don't rely on using the active sheet

    vb Code:
    1. set newsht = Sheets.Add
    2.     newsht.name = "pindexCA"
    3.     newsht.Visible = xlVeryHidden
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Object invoke error ... sometimes

    Quote Originally Posted by westconn1 View Post
    i would set the open workbook to an object
    set objwb = Workbooks.Open FileName:=sfilename
    Thanks for the reply... As I am trying to stay "fully qualified" is there a difference b/w declaring the above "objwb" as a workbook or excel.workbook?

    Can you explain why the error is happening in the first place? References have the method name conflicts or something?

    Thanks!

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Object invoke error ... sometimes

    This gives you clearer image:
    Code:
        Dim wb1 As Workbook
        Dim wb2 As Workbook
        Dim sh As Object
        
        Dim sfilename As String
        Dim temp As String
        Dim shtnext As Variant
        Dim found As Boolean
        
        sfilename = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", Title:="N-INDEX")
        If sfilename = "False" Then Exit Sub
        
        Set wb2 = Workbooks.Open(Filename:=sfilename)
        
        On Error Resume Next
        Set sh = wb2.Sheets("pindexCA")
        On Error GoTo 0
        If Not sh Is Nothing Then
            wb2.Close SaveChanges:=False
            MsgBox ("No P-Index File was found in that file.")
            Exit Sub
        End If
        
        Set wb1 = ThisWorkbook
        
        On Error Resume Next
        Application.DisplayAlerts = False
        wb1.Sheets("pindexCA").Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        sh.Copy After:=wb1.Sheets(wb1.Sheets.Count)
        wb2.Close False
        
        wb1.Sheets(wb1.Sheets.Count).Visible = xlVeryHidden
        wb1.Sheets("Driver").Activate
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Object invoke error ... sometimes

    thanks anhn this seems to be working well... just changed

    vb Code:
    1. If Not sh Is Nothing Then
    2.  
    3. to
    4.  
    5. If sh Is Nothing Then

    I don't need anything like set wb2 = nothing at the end?

    Thanks much all!

  6. #6

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Object invoke error ... sometimes

    I set sh, wb1, wb2 all to nothing at the end of the code... but still getting object invoked error.

    Quite frustrating error. Will work usually on first 2-3 times of running code then crashes with that error.. Any more help?

  7. #7

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Object invoke error ... sometimes

    My updated code is ...

  8. #8

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Object invoke error ... sometimes

    vb Code:
    1. Public Function openIndexFile(sheetName As String) As Boolean
    2. DoEvents
    3. Dim wb1 As Workbook
    4. Dim wb2 As Workbook
    5. Dim sh As Object
    6.  
    7. Dim sfilename As String
    8. Dim temp As String
    9. Dim shtnext As Variant
    10. Dim found As Boolean
    11.  
    12. Set wb1 = ThisWorkbook
    13.  
    14. For Each shtnext In Sheets
    15.     If shtnext.name = sheetName Then
    16.         Sheets(sheetName).Visible = True
    17.         Application.DisplayAlerts = False
    18.         Sheets(sheetName).Delete
    19.         Exit For
    20.     End If
    21. Next shtnext
    22.    
    23. sfilename = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls", Title:="N-INDEX")
    24. If sfilename = "False" Then Exit Function
    25.  
    26. Set wb2 = Workbooks.Open(FileName:=sfilename)
    27.  
    28. On Error Resume Next
    29. Set sh = wb2.Sheets(sheetName)
    30. On Error GoTo 0
    31. If sh Is Nothing Then
    32.     wb2.Close SaveChanges:=False
    33.     MsgBox ("No " & sheetName & " was found in that file.")
    34.     openIndexFile = False
    35.     Exit Function
    36. End If
    37.  
    38.  
    39. On Error Resume Next
    40. Application.DisplayAlerts = False
    41. wb1.Sheets(sheetName).Delete
    42. On Error GoTo 0
    43.  
    44. sh.Copy After:=wb1.Sheets(wb1.Sheets.count)
    45. wb2.Close False
    46. Set wb2 = Nothing
    47.  
    48. wb1.Sheets(sheetName).Visible = xlVeryHidden
    49. wb1.Sheets("Driver").Activate
    50. Application.DisplayAlerts = True
    51. openIndexFile = True
    52. Set wb1 = Nothing
    53. Set sh = Nothing
    54. End Function

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Object invoke error ... sometimes

    which line causes the error?

    you need some way to find out what is actually causing error, put some debug.print statements so you can tell when it crashes
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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