Solved

Killing specific processes -  I have multiple EXCEL spreadsheets running in real time.  They all need to be closed once a day automatically.  Is there a way to tag each process?

Posted on 2012-03-12
8
367 Views
Last Modified: 2012-06-22
I have multiple versions of excel running real time applications.   The programs have to be stopped and restarted during off-hours.   Some processes need to be stopped at one time and others at another time.   The processes are on several users machines.   Is there a way to identify a process so a script can be written to save and close excel for later restart.   My IT guy says he can't tell one EXCEL process from another - so he cant target which one to close down.
0
Comment
Question by:dma70
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 37710309
Yes, this can be done via excel VBA.

http://www.exceltip.com/st/Determine_if_a_workbook_is_already_open_using_VBA_in_Microsoft_Excel/472.html

I would create a generic workbook on each computer.  Then exec the workbook with the auto_open macro to close needed workbooks, saving as needed.
0
 

Author Comment

by:dma70
ID: 37710362
Not trying to determine if workbook is open.  It will be.  Trying to figure out how to identify which excel process is which.  Assume there are 10 of them and I want to shut down only one.  How does the OS script identify which process to close?
0
 
LVL 3

Accepted Solution

by:
DaFranker earned 300 total points
ID: 37710408
If you're familiar with inter-application code and the MS Office object models (Excel's in particular, of course), you could use the excel objects to specify which workbooks to close. The interface and specific method to go about doing this depends largely on which language your script will be written in.

You could also, in theory, write it (the script) in the excel sheets themselves, though the code would probably be less efficient than an external script for this particular purpose and you might end up having the additional problem of forcing your users to confirm macro activation each time one of these workbooks opens, depending on their individual security settings. Of course, if you're already using VBA code for your realtime processing in the excel documents, then this option might be worth considering more seriously.

To my knowledge, there is no way within excel to "tag" a process from the workbook without an external plugin or library being referenced, which might raise additional security concerns as and/or deployment overhead if you need to distribute the library along with the workbooks. One single process can open more than one workbook, or even none. This makes most of the data you'd normally use to identify duplicate processes volatile and unreliable.

EDIT: ged325's proposed solution would also work, and is a good simple hack solution that seems like it would suit your needs. He referred you to the instructions on detecting an open workbook simply because that's what you need to do first to guarantee that there won't be conflicts. As for identification, you would close specific workbooks rather than "excel processes", which would still achieve your objectives, since when a workbook is closed programmatically and was the last workbook of that process, the process generally shuts down.
0
 
LVL 7

Expert Comment

by:CloudedTurtle
ID: 37711484
Using C# you can loop through the processes using System.Diagnostic. You can use the "MainWindowTitle" property to determin the name of the file currently opened by that process... Here is a short snippet that loops through the Excel processes:
foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName == "EXCEL")
                {                                        
                    listBox1.Items.Add(p.MainWindowTitle);
                }                

            }

Open in new window

once you have the right process, you can use the p.Kill method to terminate the process.

Note: You could also use Office Interop to attach to the Excel Process, and save any updates and close properly instead of a Kill method. refer to MSDN OfficeInterop.Excel for more details.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 37711662
"Not trying to determine if workbook is open.  It will be."

Never assume that the program will work exactly as you intend it to.  Most errors in programs are caused by unforseen circumstances.  For instance, a user logs out their machine but leaves it on.  You call the script against that computer to close the workbook and viola, the code just broke.  Always better to be safe than sorry.


As DaFranker pointed out (thanks for the verbose explination . . . that was exactly on target), the referenced code was to find the workbook if it's open, and from there you can close the workbook forcing the save


CloudedTurtle's system.Reflection method is an elegant solution that can also accomplish the same task.  

Regardless there are multiple ways of achieving your ends.
0
 

Author Comment

by:dma70
ID: 37711721
My IT guy doesn't know c# - but I think these answers are going in the right direction.  Being able to drill down and get the name of the spreadsheet sounds like a viable solution if it were easy to program.   Seems like the best solution is to put a clock into vb code and when the right time comes shut the program down.
0
 
LVL 7

Expert Comment

by:CloudedTurtle
ID: 37711748
Handle the timing using the Windows Scheduler, this is much easier, and less problematic than having an application that is always running (less chance of memory leaks).

PS: There are equivalent methods that I showed in C# available for VB.Net as well. i just don't know them. :-)
0
 

Author Comment

by:dma70
ID: 37711770
thank you  -
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you use the right mouse button (assuming you're right handed) to click something in Windows, you usually get what is called a "context menu".  It's called that because the items in the menu vary according to context, that is, according to where…
The Display applet of Windows 7 Control Panel has changed a great deal since Windows XP  (it was missing and more or less replaced in Windows Vista by the Personalization applet.)  Below is a screenshot of what the Display applet of Windows XP, whic…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now