Thanks a lot Bruce !
That solved my problem :)
Jeff:
I dont really know what I am talking about but anyway:
This scritp dosent access the actual .xls file itself, instead it communicates
with the excel application telling it what to to with the sheet.
The code now looks like this and works just fine. If I hade the time i would
try to learn more about the correct syntax for accessing cells since this
script probably can be done in a better way. That would probably also alow me
to transfer parametrs from ansys to excel (that is not working now) but I dont
think I am going to have any need for that so Ill just let it be. Once again
thanks for the help
/Henrik
- - - - - - - - - S C R I P T - - - - - - - - -
# IronPython imports to enable Excel interop, this file shows how a parameter
is exported from excel to workbench
# Modified versin that enables interaction with the active sheet in a an open
workbook
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
from System.Runtime.InteropServices import Marshal
Excel = Marshal.GetActiveObject("Excel.Application")
def updateHandler():
#Define Key ranges in the Workbook
ExcelCell_A = worksheet.Range["A3"].Text
ExcelCell_B = worksheet.Range["B3"].Text
#Get The Workbench Parameters
# P1,P2,... not the "Name"
lengthParam_A = Parameters.GetParameter(Name="P1")
lengthParam_B = Parameters.GetParameter(Name="P2")
#Assign values to the input parameters
lengthParam_A.Expression = ExcelCell_A
#Run the project update
Update()
#Update the workbook value from the WB parameter, !!dosent work!!
ExcelCell_B = lengthParam_B
#Select the workbook and worksheet
workbook=Excel.ActiveWorkbook
worksheet=Excel.ActiveSheet
#Run the update
updateHandler()
- - - - - - - - - - - - - - - - - - - - - - - - - - -
________________________________
From: Bruce Bromberek [mailto:[email protected]]
Sent: den 16 mars 2011 18:21
To: Discussion of IronPython
Cc: Nåjde Henrik
Subject: Re: [IronPython] Reading from a open excel document
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
<[email protected]<mailto:[email protected]>> 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
[email protected]<mailto:[email protected]>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com