Consulting

Results 1 to 19 of 19

Thread: Sleeper: Kill excel instance

  1. #1
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location

    Sleeper: Kill excel instance

    I am working on a project wherin i open excel and close it but its instance is not going.
    I have set objexcel=nothing
    still its not goin>
    wat must be the error?
    can u suggest me any other method so that i can kill that excel instance which appears in the task manager.


    If intFileErrors < 1 Then
    Set ObjExcel = Nothing
    ElseIf intFileErrors = intSelectedFiles Then
    MsgBox "Alternate rows in the selected workbooks could not be shaded.", _
    vbOKOnly, "RightAnswers"
    If IsAppOpen("Excel", "Application") Then
    Set ObjExcel = Nothing
    End If
    'Some errors occured
    Else
    MsgBox "Alternate rows in " & intSelectedFiles - intFileErrors & _
    " workbooks have been shaded. Alternate rows in the following workbooks were not shaded: " & _
    vbCrLf & vbCrLf & strFileErrors, vbOKOnly, "RightAnswers"
    If IsAppOpen("Excel", "Application") Then
    Set ObjExcel = Nothing
    End If
    End If

  2. #2
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    You need to use the Quit method of the Application then Set the reference to Nothing
    Kind Regards,
    Ivan F Moala From the City of Sails

  3. #3
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    I have tried that also still its not goin.....


    'No errors occured
    If intFileErrors < 1 Then
    ObjExcel.Quit
    Set ObjExcel = Nothing
    'All insertions failed
    ElseIf intFileErrors = intSelectedFiles Then
    MsgBox "The selected workbooks were not highlighted.", vbOKOnly, "RightAnswers"
    If IsAppOpen("Excel", "Application") Then
    ObjExcel.Quit
    Set ObjExcel = Nothing
    End If
    'Some errors occured
    Else
    MsgBox "Duplicate rows in " & intSelectedFiles - intFileErrors & _
    " workbooks have been highlighted. Rows in the following workbooks were not highlighted: " _
    & vbCrLf & vbCrLf & strFileErrors, vbOKOnly, "RightAnswers"
    If IsAppOpen("Excel", "Application") Then
    ObjExcel.Quit
    Set ObjExcel = Nothing
    End If
    End If

  4. #4
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    If intFileErrors < 1 Then
    Objworkbook.Close SaveChanges:=True
    Set Objworkbook = Nothing
    ObjExcel.Quit
    Set ObjExcel = Nothing
    'All insertions failed
    ElseIf intFileErrors = intSelectedFiles Then
    MsgBox "The selected workbooks were not highlighted.", vbOKOnly, "RightAnswers"
    If IsAppOpen("Excel", "Application") Then
    ObjExcel.Quit
    Set ObjExcel = Nothing
    End If
    'Some errors occured
    Else
    MsgBox "Duplicate rows in " & intSelectedFiles - intFileErrors & _
    " workbooks have been highlighted. Rows in the following workbooks were not highlighted: " _
    & vbCrLf & vbCrLf & strFileErrors, vbOKOnly, "RightAnswers"
    If IsAppOpen("Excel", "Application") Then
    ObjExcel.Quit
    Set ObjExcel = Nothing
    End If
    End If


    still the instance is not goin.
    I have attached my project pls check it out.
    Last edited by Killian; 09-19-2005 at 08:48 AM. Reason: Attachment removed at request of OP

  5. #5
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi v_gyku,
    You need to close the spreadsheets telling if you want to save the sheets or not and then quit the app then setting the object to nothing.

    HTH

  6. #6
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    Hey guys ! I am not getting the solution.
    I need ur help guys!

    I am trying this :
    I have also tried many other things:
    Once i write the following code i think excel should go from the taskbar..
    is there any thing which i might have done because of which it might not be going?

    ObjExcel.Quit
    Set ObjExcel = Nothing

  7. #7
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    #1) Cross Posted Here

    #2) Setting the object to nothing does not close the instance. Step through the code and see what happens at each line. You should be able to figure why it doesn't close.

    Or maybe you have somehow in your code, created 2 instances of Excel, and only one is being closed. Quite possible.
    Justin Labenne

  8. #8
    VBAX Regular
    Joined
    May 2004
    Location
    Adelaide, Australia
    Posts
    28
    Location
    The other forum has been giving all the same tips.
    Maybe its the IsAppOpen function thats the problem?

  9. #9
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    I was curious about that as well, I couldn't be sure if if it was some underused vb6 function or something custom.

    v_gyku, it may be relevant to post the code you are using for that function.
    Justin Labenne

  10. #10
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    Function IsFileOpen(fileName)
    	   Dim filenum As Integer, errnum As Integer
    	   On Error Resume Next   ' Turn error checking off.
    	   filenum = FreeFile()   ' Get a free file number.
    	   ' Attempt to open the file and lock it.
    	   Open fileName For Input Lock Read As #filenum
    	   
    	   Close filenum		  ' Close the file.
    	   errnum = Err		   ' Save the error number that occurred.
    	   On Error GoTo 0		' Turn error checking back on.
    	   ' Check to see which error occurred.
    	   Select Case errnum
    		   ' No error occurred.
    		   ' File is NOT already open by another user.
    		   Case 0
    			   IsFileOpen = False
    		   ' An error occurred.
    		   Case Else
    			   IsFileOpen = True
    	   End Select
    End Function
    Function is working properly.
    I ll check if 2 obj are created or not....

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Everyone is asking about IsAppOpen not IsFileOpen.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location


    What you posted is a terrific piece of code, but it is not the function that you appear to be using in your project.

    What is the IsAppOpen functions code?

    I assume it has to exist or else your code would not get real far...............and probably not compile if your using vb6. (I think)
    Justin Labenne

  13. #13
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    ur last post help me much.
    I was using .application instead of objexcel

    Now i am closer to the solution.
    I have various conditions in my code

    -- wb is read only
    --open
    --shared

    above 3 are checked properly

    but when it comes to password protected..
    if without putting the password i press cancel then the instance dosn't go.
    otherwise putting wrong password and press ok then my excel closes and inst. goes.


    FileOpenError:
    	'Minimize the Excel window
    	ObjExcel.WindowState = xlMinimized
    	
    	Select Case Err
    		Case 4605
    			MsgBox "The """ & myFile & """ Excel document is not accessible.  The document is protected for Tracked Changes, Comments, or Forms.", vbOKOnly, "RightAnswers"
    		Case Else
    			MsgBox "The """ & myFile & """ Excel document is not accessible.", vbOKOnly, "RightAnswers"
    	End Select
    	
    	intFileErrors = intFileErrors + 1
    	strFileErrors = strFileErrors & myFile & vbCrLf
    	
       
    	Set objworkbook = Nothing
    	ObjExcel.Quit
    	cmdhighlight.Enabled = True
    after this its coming out of procedure and goes to ..

    if isappopen then
    objexcel.quit
    set objexcel=nothing

  14. #14
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    when i am checking it with F8 its working and the instance in goin.
    But when running normally it dosn't goes...
    what wld be the problem?

  15. #15
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    Well, since I don't know what IsAppOpen does, I can't really say that it is not the cause of the error.

    Probably is............how about posting it?
    Justin Labenne

  16. #16
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Here is the IsAppOpen - downloaded from the crosspost

    'Check if application is already open
    Function IsAppOpen(ProductName As String, ProductType As String) As Boolean
        Dim errnum As Integer
        Dim CreateApplication As String
        Dim myApplication
    CreateApplication = ProductName & "." & ProductType
    On Error Resume Next
        Set myApplication = GetObject(, CreateApplication)
        errnum = Err
        On Error GoTo 0
    Select Case errnum
            Case 0
                IsAppOpen = True
            Case Else
                IsAppOpen = False
        End Select
    Set myApplication = Nothing
    End Function

  17. #17
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    v_gyku I think the problem is the workbook is not being closed. Or excel is asking if you if you want to save.


    Objworkbook.Close SaveChanges:=False  '<- add this line
    Set objworkbook = Nothing
    ObjExcel.Quit
    cmdhighlight.Enabled = True

  18. #18
    VBAX Regular
    Joined
    Sep 2005
    Posts
    78
    Location
    workbook is not open then how will i close it?
    bcos when it asked for pass, i pressed cancel so the workbook is not opened...
    its givin error

  19. #19
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    You need some sort or error handling for the procedure that opens the workbook in case it doesn't open because of the password issue.
    Justin Labenne

Posting Permissions

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