Community Tip - You can change your system assigned username to something more personal in your community settings. X
Good morning all,
Im having trouble getting the following bit of code to run:
Public Sub Open_MathCAD()
Dim MathCAD As Object, Worksheets As Variant, Sheet As Variant
Set MathCAD = CreateObject("Mathcad.Application")
Set Worksheets = MathCAD.Worksheets
'THIS IS WHERE IT DOESNT WORK (I TRIED ALL OF THESE, TO NO AVAIL)
MathCAD.Options(mcAutoRecalc) = False
MathCAD.Options MCAutoRecalc = False
MathCAD.Options MCAutoRecalc = False
MathCAD.Options(MCAutoRecalc WorksheetOption) = False
End Sub
I am recieving a "Runtime Error 438 - Object doesnt support this property or method" prompt. I checked to make sure I had all the references loaded (mathcad.exe, automation.tlb, automationprivate.tlb). I did take out of this example a bit of code I used to open up an existing mathCAD file based on what the user chose with msoFileDialogFilePicker, but other than that everything's here.
All I wish to do is to turn off the automatic calculation for MathCAD. Is there something Im missing? Is my syntax right? Also, where can I find a good resource to manipulate MathCAD with VBA with code examples, other than the "Developers Reference"?
Solved! Go to Solution.
I got it! I just had to do the following to the code:
Dim MC As MathCAD.Application
Dim WST As MathCAD.Worksheets
Dim WS As MathCAD.Worksheet
Dim WIN As MathCAD.Window
Dim MathCADFile As Variant
MC.Visible = True
MathCADFile = Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Item(1)
WST.Open (MathCADFile)
Set WS = MC.ActiveWorksheet
Set WIN = MC.ActiveWindow
'to turn off AutoRecalculate
If WS.GetOption(mcAutocalc) = -1 Then
WIN.Worksheet.SetOption mcAutocalc, False
(Note that the screen should also be refreshed as it still shows AUTO in the bottom right-hand corner till you drag your mouse over it)
Thanks for your most helpful example Andy!
First of all you need to get a worksheet object (autorecalc is a property of the worksheet), then use the Worksheet.SetOption method.
Also, where can I find a good resource to manipulate MathCAD with VBA with code examples, other than the "Developers Reference"?
There really isn't one, but this document contains a lot of VBscript examples, which are at least close:
Richard,
Ok, I seemed to have gotten the MathCAD references working correctly by Dimensioning my Variables as such:
Dim MC As MathCAD.Application
Dim WST As MathCAD.Worksheets
Dim WS As MathCAD.Worksheet
Dim WIN As MathCAD.Window
...and setting the variables as such:
If MC Is Nothing = True Then
Set MC = CreateObject("Mathcad.Application")
Set WST = MC.Worksheets
End If
...
If FD.Show = True Then
MC.Visible = True
MathCADFile = Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Item(1)
WST.Open (MathCADFile)
Set WS = MC.ActiveWorksheet
Set WIN = MC.ActiveWindow
Else
Exit Sub
End If
However, I am still unable to figure out the syntax for the WS.SetOption method. Here is what I have currently tried:
WS.SetOption(mcAutocalc) = False (Gives me Argument not Optional Error)
WS.SetOption(mcAutocalc, -1) = 0 (Gives me Expected Function or Variable Error)
WS.SetOption(mcAutocalc, -1) (Gives me Compile Error: Expected = Error)
Im sure its obvious Im not sure what Im doing, so any bones you could throw me would be GREATLY appreciated. THANKS!
Hi David,
There's an example of the usage for autocalc in the set remote value function (attached).
'Turn auto calculation Off
AutoCalcMode = Application.Worksheets(w).GetOption( mcAutocalc )
Application.Worksheets(w).SetOption mcAutocalc, False
Not sure who the originator for these routines was, one of the many files that I have accumulated from the forum over the years.
It mostly works though I am getting problems when I try to open files that then open reference mathcad sheets that contain active scripts (including the ones attached...)
{There is also a warning about reading variables (GetRemoteValue) - if the variable referenced does not exist, Mathcad crashes.}
Hope this helps
Andy
I got it! I just had to do the following to the code:
Dim MC As MathCAD.Application
Dim WST As MathCAD.Worksheets
Dim WS As MathCAD.Worksheet
Dim WIN As MathCAD.Window
Dim MathCADFile As Variant
MC.Visible = True
MathCADFile = Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Item(1)
WST.Open (MathCADFile)
Set WS = MC.ActiveWorksheet
Set WIN = MC.ActiveWindow
'to turn off AutoRecalculate
If WS.GetOption(mcAutocalc) = -1 Then
WIN.Worksheet.SetOption mcAutocalc, False
(Note that the screen should also be refreshed as it still shows AUTO in the bottom right-hand corner till you drag your mouse over it)
Thanks for your most helpful example Andy!