From news.eecs.umich.edu!not-for-mail Sun Nov 28 17:10:14 1999 Path: news.eecs.umich.edu!not-for-mail From: Eric Durant Newsgroups: comp.soft-sys.matlab Subject: Re: How to open a new sheet in Excel using DDE? Date: Tue, 23 Nov 1999 16:14:27 -0500 Organization: University of Michigan EECS Lines: 55 Message-ID: <383B03B3.E22714C@umich.edu> References: NNTP-Posting-Host: 141.213.12.216 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.7 [en] (WinNT; U) X-Accept-Language: en To: Bruce Lee Xref: news.eecs.umich.edu comp.soft-sys.matlab:61720 Bruce Lee wrote: >I'm trying to use Matlab's DDE commands to open a new worksheet in >Excel (97). From the Matlab documentation, it looks like it can be >done by opening Excel with the default 'Book1' file and doing the >following: > > c = ddeinit('Excel', 'Book1') > ddeexec(c, '[Sheets.Add]') > ddeexec(c, '[ActiveSheet.Name = "matlab sheet"]') > >The link opens OK and I get a value for c but the two ddeexec >commands fail. Any ideas anyone? Is there any information about >Excel DDE on the web somewhere? The information available seems very sparse. The Matlab help headers seem to provide good information, but they are far from complete. I searched through my MSDN CDs and couldn't find a useful listing of commands that Excel accepts via DDE. For anything besides the basics, I use a kludge -- I write the function in an Excel macro and then use DDE to execute the macro. It's ugly, but it works. The Matlab syntax is: retval = ddeexec(channel, '[run("MacroName")]'); Also, the DDE links are fragile. To make the code acceptably robust, you might need some exception handling like: ============================= % Precondition: 'channel' has been initialized as a % DDE link to the Excel spreadsheet named in 'filename'. success = 0; retry = 0; while (~success), try % This block will be restarted if there is an error, % so we need to keep track of where we are (increment % a counter after each successful operation, etc.). % DDE commands here success = 1; catch retry = retry + 1; if retry > retry_limit, error('Too many DDE retries. terminating...'); end retval = ddeterm(channel); channel = ddeinit('excel', filename); end end ============================= -- Eric Durant http://www.edurant.com/