[IronPython] Reading from a open excel document

Bruce Bromberek bruce.bromberek at gmail.com
Wed Mar 16 10:20:59 PDT 2011


You might want to try
Parameter1 = worksheet.Range["A1"].Value

or

Parameter1 = worksheet.Range["A1"].Text


On Wed, Mar 16, 2011 at 9:49 AM, Nåjde Henrik <henrik.najde at volvo.com>wrote:

>  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
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20110316/bc0e3d1a/attachment.htm>


More information about the Users mailing list