Page 1 of 1

Run an Excel macro from Chemistation

Posted: Fri Sep 09, 2011 5:01 pm
by yorkflying
Does anyone knows how to run an Excel macro from Chemstation.
I am going to run a macro named “mytest” in an Excel file. Below is my code. I tried RunExcelMacro() function and DDEExecute, both of them did not work. My Chemstation version is D.01.02. Does anyone knows what is the problem? Thanks.

local excel$,myPath$,myFile$,MyString$

excel$="C:\Program Files\Microsoft Office\Office\excel.exe "
myPath$="c:\temp\"
myFile$="test.xls"

excel$=excel$ + myPath$+ myFile$
a=exec(excel$,2)

chan = ddeinitiate("excel"+val$(a), myFile$)
! DDEExecute chan, [RUN("Mytest")]
print RunExcelMacro(chan, "Mytest")

DDETerminate Chan ! Close the channel
Remove excel$,myPath$,myFile$,MyString$

Re: Run an Excel macro from Chemistation

Posted: Sun Sep 11, 2011 7:23 pm
by GasMan
I presume that version D.01.02 is the Mass Spec version of the ChemStation. I am only familiar with the GC version of the ChemStation. In the GC version, you need to address the 'system' in Excel to send commands. I have the following commands in a macro to control Excel

chan2=DDEInitiate("excel","system")
ddeexecute chan2,"[WORKBOOK.ACTIVATE(""Page 1"")]"
ddeexecute chan2,"[PRINT]"

You can try this, but please not the use of the '[' brackets, they have to be there.

Gasman

Re: Run an Excel macro from Chemistation

Posted: Tue Sep 13, 2011 7:08 pm
by yorkflying
Thanks for your help. I am using Mass Spec version Chemstation.

However, it still not working when i copied your code. It gave a DDE error:26 message. Here is the log file:

Log file is C:\MSDChem\1\log.log. Logging is on
ELSE
ENDIF
RETURN
macro "runexcel",g
TEST
local excel$,myPath$,myFile$,MyString$
excel$="C:\Program Files\Microsoft Office\Office\excel.exe "
C:\Program Files\Microsoft Office\Office\excel.exe
myPath$="c:\temp\"
c:\temp\
myFile$="test.xls"
test.xls
excel$=excel$ + myPath$+ myFile$
C:\Program Files\Microsoft Office\Office\excel.exe c:\temp\test.xls
a=exec(excel$,2)
Executing: C:\Program Files\Microsoft Office\Office\excel.exe c:\temp\test.xls
Execution completed.
2168
chan2=DDEInitiate("excel","system")
2.81858e+009
ddeexecute chan2,"[WORKBOOK.ACTIVATE(""Page 1"")]"
9/13/2011 14:58:07
Macro: TEST, Cmd: ddeexecute chan2,"[WORKBOOK.ACTIVATE(""Page 1"")]"
Msg:
DDE error: 26


endlog
logging off

Re: Run an Excel macro from Chemistation

Posted: Fri Sep 16, 2011 4:34 pm
by GasMan
yorkflying,

I can not help you any further, as my experience is with GC ChemStation macros, and there is a big difference between the Mass Spec macro language and the GC macro language.

Gasman