Solved

Detect an application instance.

Posted on 2010-08-17
7
402 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:leonstryker
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:ploftin
ID: 33457296
I would suggest you switch to VBScript.  It is much more powerful and will do what you need.
0
 
LVL 29

Author Comment

by:leonstryker
ID: 33457476
I know it can be done easialy in VBScript, but would prefer a strictly batch solution to maintain current setup.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33458829
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.




0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 29

Author Comment

by:leonstryker
ID: 33458934
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
0
 
LVL 16

Accepted Solution

by:
t0t0 earned 500 total points
ID: 33459614
That's not a problem.... Try the rest of the code for now and see if that does the trick...

Alternatively, where the user opens his excel (most likely by clicking a shortcut link to it) edit the shortcut's properites to include the '/e' switch on the command line. At that point, you could also add additional switches including the documents' working folders (always handy) as well as specifying the working directory in the shortcut too.

So for now, leave out the following line

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

and try opening files by referencing just the files themselves.
0
 
LVL 29

Author Closing Comment

by:leonstryker
ID: 33477400
Just leaving the file without Excel.exe and putting the /e switch after it did the trick. Thank you.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 33478275
oh great stuff. well done. and thank you.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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