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

How to define a variable on MathCad from an Excel.

Highlighted
Newbie

How to define a variable on MathCad from an Excel.

Hey guys, sorry in advanced if this is an ongoing discussion here, I tried hard to find something similar but I couldn't.

My problem is: I already have an mathcad file doing all the calculation i need with the necessary inputs as thickness, width, etc. I would like to be able to change these inputs using values from an excel spreadsheet.

I already figured it out how to do using the SetValue function on VBA, but the problem is that once I close the mathcad, this modification is gone. I read somewhere that this kind of manipulation is only possible changing the XML file, is that correct? Do you guys know if there is a different and simple solution for the MathCad 15?

Thanks in advance!

Denis.

7 REPLIES 7

Re: How to define a variable on MathCad from an Excel.

Hello Denis, you can follow the steps shown, to import an Excel .xlsx file.

Greetings

FM

importare un file excel.jpg

Re: How to define a variable on MathCad from an Excel.

You can modify the XML of a math region via the automation interface. I've attached a worksheet with a button that does this using a vbscript. It should be easy enough to convert the script to VBA,

Re: How to define a variable on MathCad from an Excel.

Hello Richard, thanks for your quick answer!

Unfortunatly I could not figure it out how does this worksheet work. If it is possible, could you please share more info about it?

Thanks very much!! =D

Re: How to define a variable on MathCad from an Excel.

Thanks for your answer!

By now it would be better if I didn´t have an excel file embeded on the mathcad file, I´ll try to do it from outside the mathcad.

But I´ll defenetly use this as an alternative option.

Thanks very much!

Re: How to define a variable on MathCad from an Excel.

Hi Denis.

Sorry, but can't understand what do you want. You close mathcad and modifications are gone? From where? From an Excel file? You can use inputs from an excel file, make some calculus, and save the outputs to the same excel file or another one. Both with a data component (data import wizard).

Best regards.

Re: How to define a variable on MathCad from an Excel.

It is possible to modify the XML of a math region via the automation interface. You can read or write the XML as a string to the region.MathInterface.XML property. That's easy to do as long as you know what the XML is supposed to be, and you can find the correct region in the regions collection. So, to find the region you have to set it's tag (right click on it, select properties). This piece of code:

simply steps through the worksheets regions collection until it finds the one with the correct tag, and then exits. Almost of the remaining code builds the XML for the region. It is overkill for what you want to do though, because it creates the XML from scratch, and it creates a complicated expression with a list of variable names in a matrix on the LHS and the values to assign to them in a matrix on the RHS. In your case, if you have an existing assignment all you need to do is read the XML, change the number, and write it back again. I have attached another worksheet that makes it easy to look at the XML structure for any math region. Just create the region you want to examine, give it a tag, and then point the list box at it by putting the tag in the input variable. For a simple assignment like X:=23 the XML looks like this:

You need to replace the "23" and write the XML back to the region (and then save the worksheet of course!)

Re: How to define a variable on MathCad from an Excel.

In case you're unfamiliar with controls in Mathcad, it's worth noting that the vbscript that Richard is talking about can be found by right-clicking the "Define Variables" button and selecting "Edit Script".