forgedascendant has asked for the wisdom of the Perl Monks concerning the following question:

I have a mathcad file that calculates two scaler answers based upon 3 input arrays. And I was hoping someone here would be kind enough to help me out getting this thing to work.

Looking online, I stubled across the perl module WIN32::OLE and then going through the mathcad help file it says that mathcad can interface with OLE programs to inclue those the user creates. So, basically I found the key but I am not sure how to use it. I looked over the example code given (which is for excel and not mathcad) and salvaged what I could from it.

To make sure we are on the same page lets call the input @Array1, @Array2, and @Array3 and the answers $Answer1 and $Answer2. The name of the mathcad sheet lets just call it Trial.xmcd and suppose the path if needed is c:\documents and settings\desktop.

Based upon example code I put this together so far but after this I am completly clueless on how to put everything in or get anything out out

#!/usr/ym/util/perl use Win32::OLE; #use existing instance if mathcad is already running eval {$ex = Win32::OLE->GetActiveObject('Mathcad.Application')}; die "Mathcad not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new('Mathcad.application', sub{$_[0]->Quit;}) or die "Unable to start Mathcad"; }
Thank you in advance for any help you can give,
Aaron

Replies are listed 'Best First'.
Re: WIN32::OLE interface to Mathcad
by Sinistral (Monsignor) on Apr 01, 2009 at 14:42 UTC

    You're on the right track, but you'll find that there's not a huge amount of resources for what you're doing. After a bit of Googling (for matchcad ole) I found a link describing the MathCAD OLE interface

    What you can do is to examine the MS Word and MS Excel examples for the Perl-ish syntax. The syntax tries to emulate the VBA code as much as possible within the constraints of the language

    As mentioned on the Matchcad OLE page, your best bet for finding functions and properties is to use the object browser to find "hooks".

    Good luck with it!

      I looked around and read the documentation on the Win32::OLE and looked through the many methods and properties available in mathcad. The good news is I got the perl script to open the correct sheet. The code thus far is:
      #!/usr/ym/util/perl use Win32::OLE; #use existing instance if mathcad is already running eval {$MC = Win32::OLE->GetActiveObject('Mathcad.Application')}; die "Mathcad not installed" if $@; unless (defined $MC) { $MC = Win32::OLE->new('Mathcad.Application', sub{$_[0]->Quit;}) or die "Unable to start Mathcad"; } #Figure out point of orgin and verify MC works my $h = $MC->fullname; print "$h\n"; #Open specific worksheet my $filePath = 'C:\Documents and Settings\aaron.verellen\Desktop\trial +.xmcd'; $MC->{DefaultFilePath} = $filePath; my $WS = $MC->Worksheets->Open($filePath); if ($WS->{IsOpen}) { print "Congratulations the file is open ;P\n"; } else { print "DENIED: Try opening the file again\n"; } #prove the WS object my $Name= $WS->{Name}; print "Name: $Name\n"; #Retrieve a value from the worksheet my $varable = 'number'; my $value = $WS->GetValue($varable); print "$varable = $value\n"; $WS->Recalculate; $WS->save; $WS->close; #$MC->Quit;
      This will open, recalculate the sheet, save and close the sheet. What I can't figure out is how to get a value from the mathcad sheet into the perl program. When I run the code it does not give me any errors. The results are as follows:

      C:\>perl "C:\documents and settings\aaron.verellen\desktop\trial.pl C:\Program Files\Mathsoft\Mathcad 12\mathcad.exe
      Congratulations the file is open ;P
      Name: trial.xmcd
      number =

      The mathcad sheet is simple:
      number:=2
      poly(x):=x^2+2
      poly(number)=6

      Does anyone have any guesses as to why the value is not getting back to the perl program? Again, any guesses or help is much appreciated.

      Aaron