Showing results for 
Search instead for 
Did you mean: 
Showing results for 
Search instead for 
Did you mean: 

How to define variables in Mathcad using API via Excel 2010


How to define variables in Mathcad using API via Excel 2010

Hi All,

I was hoping someone might be able to help. I'm using Mathcad 14 to perform calculation cases. I could easily have 50 sets of case in either Excel or Matlab and need to generate 50 corresponding worked through cases. Ideally I would like to generate each case file as a Mathcad file with one iteration of the case data inserted in the corresponding variable definitions.

So far I can open Mathcad using VBA in Excel 2010, write values to evaluation expressions i.e. "t = 3" but am unable to set variable definitions i.e. "t:=3". This matters because when I save the sheet any evaluation expressions that are not defined reset to 0.

I can iterate through my fifty sheets and 'get' the variable back to Excel but I need to show the 'working' on each adn thus generate the corresponding Mathcad sheet at each stage. Can anyone point me to the correct syntax (if it exists) to do this?



When you use SetValue (based on your description, I assume this is what youe are using to set the value in the worksheet) the variable is bound before any binding that is in the worksheet. So if you set a value of 3 for b, and then have a definition b:=3 in the worksheet, the variable binding from the API is prior to the worksheet definition, and is therefore overwritten by it. You cannot change a worksheet assignment using SetValue. If you really want to change a variable definition in the worksheet then you need to edit the XML of the region. The attached worksheet shows how to do this using a vbscript (which should be easy enough to port to VBA).

Many thanks for this; much appreciated. Unfortunately I'm not much of a programmer and would appreciate it if anyone has a VBA version of the script.

How did you set up your file? I'm trying to do something similar at the moment only I have absolutely no idea where to start. I can't even get mathcad to open in the VBA script.

My ultimate goal is rather simple I think, I just want a macro to open mathcad from excel and copy values from excel into the mathcad document and then save the mathcad file.


opening Mathcad from Excel isn't too difficult.

The Help files aren't too descriptive but at least it does seem to work.

Copying the values into the sheet however is a little more difficult.

Using SetValue will force the value change but in a temporary way, it isn't saved in the XML code.

You can use the regions function to access the XML code & it can be changed (as in the attached).

The downside is that mathcad doesn't know that there has been a change so you will need to explicitly save the document.

My quick code is messy & the file name needs to be created properly.

The other issue stems from how you code the XML to write.

I have taken code to assign a value to a variable & copied it into a string. to do more than this will be very complex.

Each tag in the property field must be unique but there should be no limit to the number permitted