Link to home
Start Free TrialLog in
Avatar of leonstryker
leonstrykerFlag for United States of America

asked on

Detect an application instance.

We currently have a simple batch file which when kicked off by the user opens an Excel spreadsheet in a new instance of Excel. The user requirement is to open that file in an existing instance of Excel (it makes it easier for users to copu/paste from one to the other).

Can this be done from a batch file, or do we need to switch a VBScript?
Avatar of ploftin
ploftin
Flag of United States of America image

I would suggest you switch to VBScript.  It is much more powerful and will do what you need.
Avatar of leonstryker

ASKER

I know it can be done easialy in VBScript, but would prefer a strictly batch solution to maintain current setup.
As far as i'm aware, if you start excel.exe with the command line option /e then invoke a file, followed by another file, both files will open in the single instance of excel.

I have actually tried this in Excel 2007 without the /e command line switch and it still works.

1) have your batch file open an instance of excel by specifying it's full path including the /e switch - something like this:

   "%programfiles%\microsoft office\office12\EXCEL.EXE /E"

2) then open your first excel document by reference (let's say it's called ACCOUNTS.XLS) - something like this:

   "%userprofile%\my documents\accounts\ACCOUNTS.XLS"

3) Now open your second excel document (let's say it's called OLDACC.XLS) - something like this:

   "%userprofile%\my documents\accounts\OLDACC.XLS"

Both XLS documents are now open in a single instance of Excel. You can verify this with the TASKLIST command. It only shows one instance of EXCEL.EXE running.

Putting this all together then, you might have something like this:


   @echo off
   set myfiles=%userprofile%\my documents\accounts

   "%programfiles%\microsoft office\office12\excel.exe /e"

   "%myfiles%\accounts.xls"
   "%myfiles%\oldacc.xls"


This will:

   1) open excel with the /e command line switch.
   2) set a variable (myfiles) to the path of my xls documents
   3) open accounts.xls file
   4) open oldacc.xls file

a simple check with TASKLIST will reveal just one instance of EXCEL.EXE running.




The problem is that the user will likely has an instance of Excel running before the batch starts executing, so doing:
"%programfiles%\microsoft office\office12\excel.exe /e"
would open an additional instance. Not running it we are fine except then the user does not have an instance of Excel open in which case the batch will fail.
Leon
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just leaving the file without Excel.exe and putting the /e switch after it did the trick. Thank you.
oh great stuff. well done. and thank you.