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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Externally defined symbol and '#ifndef '

giannis_tol
1-Visitor

Externally defined symbol and '#ifndef '

Hi all,

I am using OLE automation to excite my maths model with input data taken from a spreadsheet so using .SetValue, .Recalculate and .GetValue methods of the Worksheet class. For that reason I have added the name of the input variables right at the top pf the file without the := operator.

My problem is that I want my MathCad model to be also a standalone model where it will look if automation is active or not, and when not, it will have a local definition with :=

To do that in C, we would define a global symbol when in automation:

#define OLE_AUTOMATION

And then in the program we would add a conditional compilation branch

#ifndef OLE_AUTOMATION

int variable_input_x;

variable_input_x = y;

#endif

Is there any way of doing something similar in Mathcad?

Many thanks

ACCEPTED SOLUTION

Accepted Solutions
RichardJ
19-Tanzanite
(To:giannis_tol)

Is there any way of doing something similar in Mathcad?

Sort of. There is a way (although it is a kludge!) to trap an undefined variable: see the attached worksheet. That means you you can set up the worksheet to use the input variable if it's defined via the OLE interface, or a default value it it's not.

View solution in original post

12 REPLIES 12
RichardJ
19-Tanzanite
(To:giannis_tol)

Is there any way of doing something similar in Mathcad?

Sort of. There is a way (although it is a kludge!) to trap an undefined variable: see the attached worksheet. That means you you can set up the worksheet to use the input variable if it's defined via the OLE interface, or a default value it it's not.

That's great Richard, thank's a lot

I am afraid I still need some help to finish this off as it seems that I 've hit another wall.

Firstly, when I am trying to add a Mathcad objectusing Excel's Object wizard, 'the create from File' tab allows me to insert a MathCad worksheet as an object that appears minimized but always fails to open when double clicking on it. THe only way I can insert a MathCad worksheet is through the 'Crete New' tab and selecting Mathcad Document type.

So I have a blank worksheet where I pasted the contents of your attachement, but unfortunately this does not work either as components and scripts of a mathcad worksheet do not appear at all in an embedded Mathcad object in Excel (look at the attached image, the 'Controls' toolbar do not appear at all in the dropdown menu)

and I am 'Null pointer encountered' error messages...(again see attached image no 2)

I am using Mathcad 14.0 M020 (14.0.2.5 [802141434]) and Excel 2010 (14.0.6023.1000, 32-bit).

I searched in the forum for anything relevant but I can't seem to be able to find anything.

Have you got any suggestions?

Image no 1:

Untitled.jpg

Im. No 2:

Untitled2.jpg

RichardJ
19-Tanzanite
(To:giannis_tol)

True, components do not appear to work in a Mathcad worksheet embedded in an Excel document. If you have embedded the worksheet in an Excel document why do you want to do this anyway though? If it's embedded in Excel, it cannot also be a standalone worksheet.

You have a very good point, but my problem is that Mathcad 14 OLE does not seem to work OK in Excel 2010.

When I try to add a Mathcad OLE object from an existing .mcd/.xmcd file, the created object is not recognised as a Mathcad object hence I cannot access the Worksheet class to perform my IO access.

Is there anything that I am missing? How can I actually insert your workbook (direclty or as a link) and not insert a new embedded Mathcad object in Excel 2010?

If you want to take data stored in an excel spreadsheet, one option is to 'READEXCEL' as attached.

If the file does not exist the on error allows you to preset a value or set the NaN (not a number) flag.

regards

Andy

Hi Andy,

Thanks for your suggestion.

I don't just want to read data stored in a spreadsheet, but rather take some data in, recalculate and then extract some data out and do that in a loop. I have successfuly done that using embedded mathcad OLE, but this is a problem since I want to have a single referenced file that will be common throughout and also will not have to worry about version control, people randomly editing the worksheet etc.

In any case, 'READEXCEL' seems to be a MC15 addition and I only have MC14 available...

RichardJ
19-Tanzanite
(To:giannis_tol)

If you want a single referenced file then you don't want to embed a Mathcad worksheet at all. You just want to call it from VBA. Here's a really basic example written in Excel 2000. Does this work with Excel 2010?

Hi Richard,

I can't seem to find any VBA code anywhere in that spreadsheet(Automation example3.xls).

In fact I saw that file yesterday on another post and I had the same issue...

Don't know if that's because of excel2010 blocking something or my settings or some other reason.

Would it be possible to get a copy of the subs code here?

Thanks again for all your efforts

RichardJ
19-Tanzanite
(To:giannis_tol)

If you go into the VBA editor it should be right there. I think you have to install something extra in Office 2010 though. I'm not sure because I am too cheap to upgrade from Office 2000

Anyway, here's the code. There is one button and one textbox that's used for the input.

Private Sub CommandButton1_Click()

Set objMCApp = CreateObject("Mathcad.Application")

objMCApp.Visible = True

Set objMCWks = objMCApp.Worksheets.Open("C:\Data\Mathcad\automation_example3.mcd")

objMCWks.SetValue "MyInput", CDbl(TextBox1.Text)

Set objResult = objMCWks.GetValue("MyOutput")

For i = 0 To objResult.Rows - 1

For j = 0 To objResult.Cols - 1

MsgBox objResult.GetElement(i, j)

Next

Next

'objMCApp.Quit (mcDiscardChanges)

End Sub

The button and the textbox are missing, I just realized that a few of my Office 2003 macros are missing when I open them with Office 2010 so there is definitely some sort of version incompatibility there.

Thanks for your help, it got me where I wanted to be, really appreciate it.

I have been experimenting with the example files in the qsheet folder, always good to learn new techniques.

so far I have managed to follow most of the process to link the data in & out of a simple embeded mathcad object (almost),

But ... 2 significant glitches:

1. when the XLSM sheet is opened, XL does not recognise the object , however if you right click on the object & open or edit the embeded sheet

then XL decides that it is OK & will run the macro

2. for the example sheet 'trajectory' modifying the input parameters 'velocity' or 'angle' will cause the macro to run & auto update the whole sheet.

on my new version you have to manually update for each value change.

In both cases I can't see the obvious difference.

Anyone have any suggestions?

Regards

Andy

Announcements

Top Tags