I am having trouble closing excel when I execute the following code:
#!/usr/ym/util/perl use Win32::OLE; #Perl "C:/documents and settings/aaron.verellen/desktop/trial3.pl" Win32::OLE->Option(Warn => 2); #use existing instance if excel is already running eval {$EX = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $EX) { $EX = Win32::OLE->new('Excel.Application', sub{$_[0]->Quit;}) or die "Unable to start Excel"; } #Open specific worksheet and tab my $filePath = 'C:\Documents and Settings\aaron.verellen\Desktop\MathC +ad Bridge.xls'; my $WB = $EX->workbooks->Open($filePath); my $S = $WB->worksheets("Caculations"); $S->activate(); #Set values on the worksheet $S->range("A1")->{value} = 16; $S->range("B1")->{value} = 17; $S->range("C1")->{value} = 18; #call macro to process data $EX->run("Bridge_To_MathCad"); #get the answers from the worksheet and close the excel sheet $output1 = $S->range("A2")->{value}; $output2 = $S->range("b2")->{value}; #close excel sheet $WB->close; #return the values print "($output1, $output2)\n";
What happens is that the excel window (named MathCad Bridge.xls) will not close like I want. Instead the file reads (Mathcad in MathCad Bridge.xls). What I can tell is that the mathcad object is still open from where the macro opened it. The macro is as follows (VBA code):
Sub Bridge_To_MathCad() Dim outA, outB As Variant Dim inX, inY, inZ As Variant Dim Value1, Value2 As Variant 'Read in values to be passed from Excel to Mathcad inX = ActiveSheet.Range("A1").Value inY = ActiveSheet.Range("B1").Value inZ = ActiveSheet.Range("c1").Value 'Get a hold of the mathcad object Dim MathcadObject As OLEObject Set MathcadObject = ActiveSheet.OLEObjects(1) MathcadObject.Activate 'Get a hold of the mathcad worksheet Set Ws = MathcadObject.Object.Worksheet 'hand the values over to Mathcad, assign them to varables 'recalculate, and read the results into excal Ws.setValue "x", inX Ws.setValue "y", inY Ws.setValue "z", inZ Ws.Recalculate 'Place the result values into the chosen Excel cells ActiveSheet.Range("A2").Value = Ws.getvalue("output1") ActiveSheet.Range("B2").Value = Ws.getvalue("output2") 'Return to Excel sheet ActiveSheet.Select End Sub
Am I forgetting something? Any help would be appreciated,

Aaron


In reply to Win32::OLE Closing Excel by forgedascendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.