. .


Using Excel from ObjectStudio

March 17, 2010 8:10:13.306

Yesterday, I pushed up a short walkthrough on using Excel from VisualWorks; this morning I did a screencast on using Excel from ObjectStudio. Here's the walkthrough for that piece.

First, you'll need to load the required support. Using File>>Load Application in the launcher menu, select "OLE" and load:

Once you have that loaded, open a workspace (from the Tools menu) and add the following script - you can start exploring the COM APIs from here and tweak the script as needed:

| ole excel wb range value |
"get the dispatcher for Excel"
ole := OLEObject newProgId: 'Excel.Application'.
excel := ole dispatcher.

"open Excel and get a worksheet"
excel call: 'Visible' params: (Array with: true).
wb := excel call: 'Workbooks' params: #().
wb call: 'Add' params: #().

"Now add some data"
range := excel call: 'Range' params: (Array with: 'A1').
range at: 'Value' put: 'Fred'.
range := excel call: 'Range' params: (Array with: 'A2').
range at: 'Value' put: 'Flintstone'.

"make the range bold"
range := excel call: 'Range' params: (Array with: 'A1:A2').
(range at: 'Font') at: 'Bold' put: (Array with: true).

"Now read a cell back"
range := excel call: 'Range' params: (Array with: 'A1').
value := range at: 'Value'.

Unlike VisualWorks, you don't need to highlight that code - simply pull down the Smalltalk menu in the workspace and try "Insert" (which will print the results into the workspace) - you should end up with Excel opened up, with "Fred" in A1 and "Flintstone" in A2, and "Fred" in your workspace at the bottom - like this:

That's pretty much it - you can explore the available COM APIs and work from there - but this should get you started.

Technorati Tags: , , ,

posted by James Robertson

 Share Tweet This