I want to force close word file if it is open in windows before running my code.
Found following code with DDE on SAS site but its not working and I am unable to close "myfile.rtf"
Can someone please clarify ?
https://support.sas.com/kb/40/191.html
/* Create a sample table routing to RTF using ODS */
ods rtf file="myfile.rtf";
proc tabulate data=sashelp.class;
class age sex;
table age,sex;
run;
ods rtf close;
/* Use DDE to make the request to Microsoft Word */
filename word dde 'winword|system';
data _null_;
file word;
/* The following PUT statements execute Microsoft Word commands */
put '[FileClose 2]';
put '[FileExit 2]';
run;
DDE won't work at all if you are using SAS on a remote server. It will only possibly work if you use SAS installed on your PC and even then it is not recommended.
In my environment, I was able to close the rtf file with the code using DDE.
However, in the case of the code you presented, the rtf file should not have been opened at the time the DDE session was established, so there is an error.
Are there any errors in the log?
Also, instead of using dde to close the file, you can prevent the ods output result (rtf file) from being opened by writing the following code before ods statement.
ods noresults;
Please note that DDE is not really advocated by Microsoft and there are other applications that you might have installed that use the communication channels that DDE uses and may well interfere with them. So DDE approaches are extremely fragile and may well not work, not because of the SAS code but the operating system environment.
At least one application that interferes is Cisco Jabber. If the Jabber service is running then DDE, at least the limited amount that I used, would not work. If you have Jabber the service must be KILLED, as in kill process in task manager not just close an active window, before DDE may work.
DDE won't work at all if you are using SAS on a remote server. It will only possibly work if you use SAS installed on your PC and even then it is not recommended.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.