Visual Basic Example

This example uses a simple Visual Basic 6.0 program and the Automation Embedding API to set the variable in0 in a Mathcad OLE object to a complex number, allow Mathcad to perform a calculation, and put the answer into a TextBox control.

Step 1: Add a Reference to the Mathcad object to the object browser.

  1. Choose References from the Project menu.
  2. In the list of Available References, select Mathcad.
  3. Make sure the check box to the left of Mathcad is checked and click "OK."
  4. To view Mathcad's object model, choose Object Browser from the View menu and select Mathcad in the list of available libraries.

Step 2: Design the form. For example, your form might look like this:

Using the Automation interface to control Mathcad from a VB application

On the left is the Mathcad Document object within an OLE Container control. It contains statements to show the input, perform a single calculation, and display the output. On the right are text boxes to provide input and view output from the Mathcad object. There is also a CommandButton control used to submit the input to Mathcad, recalculate, and extract the output to the output TextBox controls.

Step 3: Write the code.

Private Sub cmdSubmit_Click()
Dim objMC As Object
Dim varInReal As Variant, varInImag As Variant
Dim varOutReal As Variant, varOutImag As Variant

'set a reference to the Mathcad object in the OLE container
Set objMC = OLE1.Object

'pass data to Mathcad worksheet via the SetComplex method
'getting inputs from the contents of the text boxes
'NOTE: normally you would validate inputs here prior to conversion from string values


varInReal = CDbl(txtInReal.Text)
varInImag = CDbl(txtInImag.Text)
Call objMC.SetComplex("in0", varInReal, varInImag)

'recalculate worksheet
Call objMC.Recalculate

'retrieve data from Mathcad using the GetComplex method
Call objMC.GetComplex("out0", varOutReal, varOutImag)

'assign outputs to text boxes
txtOutReal.Text = CStr(varOutReal)
txtOutImag.Text = CStr(varOutImag)

End Sub

Note: