emorris1000
Registered User.
- Local time
- Today, 03:16
- Joined
- Feb 22, 2011
- Messages
- 125
I have a program I have written that does a bunch of wacky stuff in excel. Before the program runs though, it is imperative that it runs through and closes ALL instances of excel before executing. This is due to an issue with the files that are going to be manipulated are often open on peoples desktops, which messes up the program.
Originally I just used the following line and would have to go to the taskbar and manually shut down any visible or invisible instances of excel:
Now I am trying to make something a little less derp. This is what I have, but it has a couple of problems:
Now, this works, letting me go throught the open files and gives the user the opportunity to close them, but on the last loop it crashes on the xlapp.visible line (saying there is no application) and leaves an invisible instance of Excel.
If I run it without any instances of excel open it crashes on the getobject command. This is expected, but the on error should send it to the if command.
Not sure I entirely understand the getobject command. Everywhere I read it says that its only there to find an existing instance, but I could SWEAR that it is creating them in some situations.
seems that the on error is the main issue....
Edit: Ok I am dumb. It was set to break on all errors.....
Originally I just used the following line and would have to go to the taskbar and manually shut down any visible or invisible instances of excel:
Code:
Set xlApp = GetObject("", "Excel.Application")
Now I am trying to make something a little less derp. This is what I have, but it has a couple of problems:
Code:
Public Sub CloseAllExcel()
On Error Resume Next
Dim xlApp As Excel.Application
Dim i As Integer
i = 0
Do While i < 50 'to avoid infinite loop
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Exit Do
End If
xlApp.Visible = True
xlApp.DisplayAlerts = True
xlApp.Quit
Loop
End Sub
Now, this works, letting me go throught the open files and gives the user the opportunity to close them, but on the last loop it crashes on the xlapp.visible line (saying there is no application) and leaves an invisible instance of Excel.
If I run it without any instances of excel open it crashes on the getobject command. This is expected, but the on error should send it to the if command.
Not sure I entirely understand the getobject command. Everywhere I read it says that its only there to find an existing instance, but I could SWEAR that it is creating them in some situations.
seems that the on error is the main issue....
Edit: Ok I am dumb. It was set to break on all errors.....
Last edited: