Closing a Specific MS Excel Instance

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Does anyone have any VBA code that allows me to iterate through several MS Excel instances and kill the instance based on a specific MS Excel file that is open? Apologies if this has been answered in the past. I did do a search but couldn't see anything specific to what I wanted.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
based on a specific MS Excel file that is open

If the workbook opened in the instance in question was previously saved to disk, you should simple be able to use GetObject using the full path of the file.
There would be no need to iterate.

If the workbook opened in the instance in question has yet to be saved, this requires a bit more legwork including iterating through open instances which is no trivial pursuit AFAIK. There is example code on this board which does so.

Before continuing, which is it?
 
Upvote 0
The latter. If you can point me to any code within this forum, that would be great. I have spent almost a week on this. Whoever solves this for, I will be eternally grateful.
 
Upvote 0
There didn't appear to be anything on that thread that helped. Also some of the links don't work anymore.
 
Upvote 0
There seems to plenty in that thread that applies to your question here and now. Have you made any attempt? Please post what you have tried thus far.
 
Upvote 0
@azizrasul, indeed like dataluver said post your attempt, so we can see what you've tried so far.

As an indirect solution to this question, I ran in to a similar issue and indirectly solved it using the VBA "appactivate()" statement which relied indirectly on the Excel filenames of opened Excel windows connected to those opened Excel filenames rather than direct Excel instances. I imagine the direct solution to this question of using Excel instances would be equivalent to identifying the "PID" of each Excel instance and somehow connecting that PID to the filename associated with that respective opened Excel filenames in its respective Excel window, and then execute the code to close that respective Excel instance via killing that respective "PID" connected to that respective Excel window of that respective Excel filename opened.
 
Upvote 0
Thanks for that. Pretty busy doing some DIY around the house. Will get back in the evening.
 
Upvote 0
I'm not sure that we are on the same page. This, adapted from the link I referred to, will fetch a reference to the application hosting an unsaved workbook named "Book1".

VBA Code:
Sub Example()
    Dim xl As Application
   
    Set xl = GetObject("Book1").Application
    xl.Workbooks(1).Close False
    xl.Quit
End Sub
 
Upvote 0
I tried the above code in my simple example to see if it works and it does. Great.
Will try to incorporate in my project and see if it still works. Many thanks in the meantime. Will get back to you in the next few days if it doesn't work. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top