Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Python Excel
i am working project where ANAYS WORKBENCH is interfaced with MS EXCEL via python script.
 
    # IronPython imports to enable Excel interop
    import clr
    clr.AddReference("Microsoft.Office.Interop.Excel")
    import Microsoft.Office.Interop.Excel as Excel
    
    workingDir = AbsUserPathName("C:\Users\shreepal\Downloads\Compressed/")
    
    def updateHandler():
    
    	# Define key ranges in the Workbook
    	lenCell = worksheet.Range["B6"]
    	ldCell = worksheet.Range["B7"]
    	dfCell = worksheet.Range["B8"]
    
    	# Get the Workbench Parameters
    	lenParam = Parameters.GetParameter(Name="P1")
    	ldParam = Parameters.GetParameter(Name="P2")
    	dfParam = Parameters.GetParameter(Name="P3")
    
    	# Assign values to the input parameters
    	lenParam.Expression = lenCell.Value2.ToString()
    	ldParam.Expression = ldCell.Value2.ToString() 
    	
    	# Mark the deformation parameter as updating in the workbook
    	dfCell.Value2="Updating..."
    
    	# Run the project update
    	Update()
    
    	# Update the workbook value from the WB parameter
    	dfCell.Value2 = dfParam.Value.Value
    
    
    # Open the Workbench Project
    Open(FilePath = workingDir + "ExcelParameterScripting.wbpj")
    
    # Open Excel and the workbook
    ex = Excel.ApplicationClass()
    ex.Visible = True
    workbook = ex.Workbooks.Open(workingDir + "ParameterExample.xlsm")
    worksheet=workbook.ActiveSheet
    
    #Apply the update handler to the workbook button
    OLEbutton = worksheet.OLEObjects("CommandButton1")
    OLEbutton.Object.CLICK += updateHandler

 


This code will launch Excel and Workbench and add the command to execute the update script when we push the button in excel.
 
**problem is that the action to update the spreadsheet and model is assigned as an OLEObject to the button. Unfortunately this is only active when the script is running. Once it is done the click action is removed, and the button becomes useless. can anyone provide me the code for this button which will able to update the system as many times it is clicked?**
Posted 11-Apr-13 1:15am
Edited 11-Apr-13 4:20am
(no name)151.2K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100