Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29251 Discussions

Call to Excel fails when Excel already open

nvaneck
New Contributor I
1,023 Views

I have the following code to invoke Excel, where filename is the name of the form name.csv.
However, it won't work if Excel is already open. Is ther any way to get around this?
If Excel is already open, I=SHELLEXECUTEEX(SEI) just returns immediately without any waiting. Otherwise it works fine.

 

       CALL ZEROMEMORY(LOC(SEI),SIZEOF(SEI)) 
       SEI.CBSIZE = SIZEOF(SEI)
       SEI.FMASK = SEE_MASK_NOCLOSEPROCESS
       SEI.LPFILE = %LOC(FILENAME) 
       I=SHELLEXECUTEEX(SEI)
      IF (I .EQ. 0) THEN
         I = GETLASTERROR()
      ELSE
         CALL SYSOUT('0Waiting for Excel to finish...\(If Excel fails to start you may already have it open and need to close it.)')
         I = WAITFORSINGLEOBJECT(SEI.HPROCESS, INFINITE)
         I = CLOSEHANDLE(SEI.HPROCESS)
      END IF

0 Kudos
5 Replies
andrew_4619
Honored Contributor III
1,023 Views

I do not know the behaviouir of ShellExecuteEx but I use CreateProcess to launch excel with a csv file and it does not care if excel is already open as another instance of excel is created.

0 Kudos
nvaneck
New Contributor I
1,023 Views

I use SHELLEAXECUTEEX because I can't determine the path to whatever version of Excel or equivalent spreadsheet program is on the user machine.  In my current code I supply the filename and the system finds the program associated with csv files.  Prior to thjis code segmnent, I print a message to the user requesting them to make sure Excel is not running before calling SHELLEXECUTEEX, but this is kludgy.  If there is a way to prevent continued execution if it fails, I could use that, but the error handling in my code doesn't catch failure to execute.  Is there some way to improve this?

 

0 Kudos
nvaneck
New Contributor I
1,023 Views

BTW, it does launch a separate instance of Excel, but it doesn't bring up the file to the screen and just returns error code -1 without waiting for action by the user.

0 Kudos
andrew_4619
Honored Contributor III
1,023 Views

Create process always opens the csv file for me irrespective of if excel is already open. You can get the install path for excel from the regfistry:

HKEY_LOCAL_MACHINE\Software\Classes\Excel.Application\CLSID

and, depending on what version of Excel is installed, you would find that the CLSID for Excel.Application is "{00020841-0000-0000-C000-000000000046}." Next, using this CLSID, you would examine the following registry key to find the path for EXCEL.EXE:

HKEY_LOCAL_MACHINE\Software\Classes\CLSID\{00020841-0000-0000-C000-000000000046}\LocalServer32

This is a sample code at http://support.microsoft.com/kb/240794

0 Kudos
nvaneck
New Contributor I
1,023 Views

Thanks! I'll give it a try...

0 Kudos
Reply