PDA

View Full Version : Sleeper: Kill excel instance



v_gyku
09-11-2005, 11:04 PM
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

Ivan F Moala
09-11-2005, 11:24 PM
You need to use the Quit method of the Application then Set the reference to Nothing

v_gyku
09-11-2005, 11:30 PM
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

v_gyku
09-11-2005, 11:52 PM
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.

Tommy
09-12-2005, 06:17 AM
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

v_gyku
09-15-2005, 04:46 AM
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

Justinlabenne
09-15-2005, 05:06 AM
#1) Cross Posted Here (http://www.vbforums.com/showthread.php?t=359928)

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

Insomniac
09-15-2005, 05:17 AM
The other forum has been giving all the same tips.
Maybe its the IsAppOpen function thats the problem?

Justinlabenne
09-15-2005, 05:21 AM
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.

v_gyku
09-15-2005, 05:25 AM
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....

Bob Phillips
09-15-2005, 05:33 AM
Everyone is asking about IsAppOpen not IsFileOpen.

Justinlabenne
09-15-2005, 05:40 AM
http://vbaexpress.com/forum/images/smilies/119.gif

What you posted is a terrific piece of code (http://vbaexpress.com/kb/getarticle.php?kb_id=468), 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)

v_gyku
09-15-2005, 06:31 AM
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

v_gyku
09-15-2005, 06:58 AM
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?

Justinlabenne
09-15-2005, 07:01 AM
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?

Tommy
09-15-2005, 07:10 AM
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

Tommy
09-15-2005, 07:21 AM
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

v_gyku
09-15-2005, 07:29 AM
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

Justinlabenne
09-15-2005, 01:52 PM
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.