[IronPython] Reading from a open excel document

Nåjde Henrik henrik.najde at volvo.com
Wed Mar 16 15:49:22 CET 2011


Hello!

I am trying to transfer prameters from an excel sheet to ANSYS Workbench (a FEA software), the code is run as a macro in ANSYS workbench.

I have adapted a code that is opening an already existing document and successfully transfers parameters from it, so I know that what i want to do can be done.
The problem is that I cant access a already open document and read from it, using instructions from http://www.ironpython.info/index.php/Interacting_with_Excel
For error-seraching I have created a new code looking like this:

- - - - - - - - - - - - - - - - - -
#Part 1
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")

from System.Runtime.InteropServices import Marshal
Excel = Marshal.GetActiveObject("Excel.Application")

#Part 2
workbook = Excel.ActiveWorkbook     #gives no error
worksheet = workbook.ActiveSheet     #errror

worksheet = Excel.ActiveSheet          #no error
Parameter1 = worksheet.Range["A1"]    # error

- - - - - - - - - - - - - - - - - -

The problem is that when trying to access a excel "workbook" with the command "worksheet=workbook.ActiveSheet" as descriped in the above link I get an error message.

If i instead chose to use "worksheet = Excel.ActiveSheet" I get no error message, but when I continue to access a cell on the active sheet, the script still fails.
I am trying to read the cell using "Parameter1 = worksheet.Range["A3"]"

It seems to me that the script fails at the point where I try to select the workbook or the worksheet, but I have really no clue.

Does anyone know the correct way to access an open workbook and read from the active sheet?


/Henrik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110316/84342fa3/attachment.html>


More information about the Ironpython-users mailing list