11
$\begingroup$

This post 4594 shows how to open a existent Excel file with Mathematica. And this 6686886 shows how to do what I need, but in C#. This 12282310 is another related one.

How can I connect to a spreadsheet that is already open?

$\endgroup$
12
$\begingroup$

Using SE as my rubber duck to organize ideas worked just nice. I found the answer in NETLinkUserGuide.

To get current Excel session use GetActiveCOMObject instead of CreateCOMObject. Here is a code example.

Needs["NETLink`"]
InstallNET[]
excel=GetActiveCOMObject["Excel.Application"]
workbook=excel@Workbooks["MySpreadsheet.xlsx"];
worksheet=workbook@Worksheets["Sheet1"];
worksheet@Range["A1"]@Value="1, 2, 3 testing"

To get your active workbook use:

With[{$name=excel@ActiveWorkbook@Name},
    workbook=excel@Workbooks[$name]
]

instead of:

workbook=excel@Workbooks["MySpreadsheet.xlsx"];

PS: any tip to replace code injection using With is welcome.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Humanity needs more Ducks (plus 1 Quacker). $\endgroup$ – user9660 Apr 18 '15 at 5:46
  • $\begingroup$ +1 Seems promising! Its a pity that all this isn't more explored by WR. After their ?acquisition? of the "link for excel", I was hoping for its inclusion in standard Mathematica... and, who knows, allowing for Enterprise CDF to work as an Excel computational kernel on previously signed user functions. With the Enterprise, I always have the feeling that I'm paying too much for too little... $\endgroup$ – P. Fonseca Apr 18 '15 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.