Test if Excel is Open In General and Close if so

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

Test if Excel is Open In General and Close if so

Hello all. I know how to close excel from sas, using the code below, however I can not figure out how to test to see if any instance of excel is open before testing for this close.

I have tried using the fid=fopen('LIBNAME'), however it always returns a value of zero, even when Excel is open. Example, I want the following code to return a value of 1 if any instance of Excel is running, but return a zero otherwise (so I can run this code in a subset). I have tried using general datasets and macro coding, but neither have worked.

Code I want to work here

filename EXCEL1 dde 'excel|system';

data MYDATA;

fid=fopen('EXCEL1');

run;

Current Code Below

filename EXCEL1 dde 'excel|system';

data _null_;

file cmd;

put '[quit()]';

run;

filename cmd clear


Accepted Solutions
Solution
‎01-03-2013 12:39 PM
PROC Star
Posts: 7,468

Re: Test if Excel is Open In General and Close if so

Posted in reply to Anotherdream

Since you used DDE in your example, can I presume that you are on some version of Windows?

If so, you could use a pipe to run TASKLIST and simply check the results.  e.g.:

filename programs pipe "tasklist /svc";

data want;

  infile programs truncover;

  informat open_programs $80.;

  input open_programs;

  if open_programs eq "EXCEL.EXE" then

    put "Excel is open";

run;

View solution in original post


All Replies
Solution
‎01-03-2013 12:39 PM
PROC Star
Posts: 7,468

Re: Test if Excel is Open In General and Close if so

Posted in reply to Anotherdream

Since you used DDE in your example, can I presume that you are on some version of Windows?

If so, you could use a pipe to run TASKLIST and simply check the results.  e.g.:

filename programs pipe "tasklist /svc";

data want;

  infile programs truncover;

  informat open_programs $80.;

  input open_programs;

  if open_programs eq "EXCEL.EXE" then

    put "Excel is open";

run;

Super User
Super User
Posts: 7,039

Re: Test if Excel is Open In General and Close if so

You can also use the /FI option on the tasklist command.

filename excelchk pipe %sysfunc(quote(tasklist /fi "imagename eq excel.exe")) ;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 644 views
  • 0 likes
  • 3 in conversation