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

Hello, I am new here and to perl. How do I write the code to cancel an excel copy selection. In VBA it would be "Application.CutCopyMode = False". How do I write this in perl? Thank you.

Replies are listed 'Best First'.
Re: coding for excel in perl
by davies (Monsignor) on Dec 27, 2009 at 17:27 UTC
    There are two things you might want to try. One is using 1 and 0 instead of true and false - I'm not sure how Win32::OLE will parse False. The other is that some Excel properties need to be in {braces}. The rules for which need braces and which don't seem to have something to do with the checksum of the property being divisible by Bill Gates's birthday. So I would try (using strict and all the other fruit):

    $xl = Win32::OLE->new('Excel.Application'); $xl->{CutCopyMode} = 0;
    But you may need to get rid of the braces - I haven't tested this.

    Regards,

    John Davies
      Thank you, John. You are so right! I had done some trial and error and came up with that same line. Came online to let everyone know. It works perfectly. Thank you so much for your response.

      david

Re: coding for excel in perl
by Corion (Patriarch) on Dec 27, 2009 at 15:22 UTC

    Basically, you use Win32::OLE to make the same calls from Perl that you would do from VBA. The Win32::OLE documentation shows how to translate the calls.

Re: coding for excel in perl
by CountZero (Bishop) on Dec 27, 2009 at 15:08 UTC
    Welcome to the Monastery ruxer!

    It is customary that you show some relevant example code you have written yourself. In this case --for instance-- a small Perl-script which opens an Excel-file, selects and put in the copy buffer some cells and then needs to be completed with the code to release the copy-selection.

    You are much more likely to receive favourable answers if you show that you have at least tried but failed, rather than having failed for not having tried at all. It is not a shame to fail, but it is a sin in our Monastery not to try.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thank you CountZero!

      The following --portion of script-- is copying from one sheet and pasting to another to prepare for print. Everything works beautifully except for the line "$Excel->Application->CutCopyMode = False;".

      I have tried writing the line several different ways and I have searched google and multiple other sights for ideas.

      Opening of script is:

      use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use warnings; #Content-Type: text/html; $Win32::OLE::Warn = 3; # die on errors... # get already active Excel application or open new $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # open Excel file $Book = $Excel->Workbooks->Open("c:/users/david/desktop/RBLCopy.xls");
      The following is the portion of script containing the line in question.
      # print routine ################ print "\nPrint (y/n) "; chomp($input = <STDIN>); if ($input eq 'y') { $lSheet = $Book->Worksheets("Location"); $iSheet = $Book->Worksheets("Input"); $oSheet = $Book->Worksheets("Output"); # Clear Input screen $iSheet->Activate(); if ($iSheet->Range("C4")->{Value} ne '') { $iSheet->Range("C4:J10000")->ClearContents(); } # Move Location data to Input Screen $lSheet->Activate(); $lSheet->Range("A5:A10000")->Copy(); $iSheet->Activate(); $iSheet->Range("C4")->Select(); $iSheet->Paste(); $lSheet->Activate(); $lSheet->Range("L5:L10000")->Copy(); $iSheet->Activate(); $iSheet->Range("D4")->Select(); $iSheet->Paste(); $lSheet->Activate(); $lSheet->Range("E5:J10000")->Copy(); $iSheet->Activate(); $iSheet->Range("E4")->Select(); $iSheet->Paste(); $iSheet->Range("A1")->Select(); $lSheet->Activate(); $Excel->Application->CutCopyMode = False; $lSheet->Range("A1")->Select(); print "\nAre you ready to print? (y/n)"; chomp($input = <STDIN>); if ($input eq 'y') { # print page 1 $oSheet->Activate(); $Excel->ActiveWindow->SelectedSheets->Printout(); # print page 2 $iSheet->Activate(); if ($iSheet->Range('C34')->{Value} ne '') { $iSheet->Range("C34:K63")->Copy(); $iSheet->Range("C4")->Select(); $iSheet->Paste(); $oSheets->Activate(); $oSheets->Range("K1")->Select(); $Excel->ActiveWindow->SelectedSheets->Printout(); } # print page 3 $iSheet->Activate(); if ($iSheet->Range('C64')->{Value} ne '') { $iSheet->Range("C64:K93")->Copy(); $iSheet->Range("C4")->Select(); $iSheet->Paste(); $oSheets->Activate(); $oSheets->Range("K1")->Select(); $Excel->ActiveWindow->SelectedSheets->Printout(); } } }
      Thank you in advance for any help you can give.

      david

Re: coding for excel in perl
by ruxer (Acolyte) on Dec 27, 2009 at 18:09 UTC
    Just to let everyone know ...

    Given:

    $Excel = Win32::OLE->new('Excel.Application');
    The VBA code "Application.CutCopyMode = False" in perl is:
    $Excel->{CutCopyMode} = 0;
    Thanks to all who responded, especially davies.

    ruxer

Re: coding for excel in perl
by jeffreyray (Sexton) on Dec 28, 2009 at 15:52 UTC

    If you plan to use Win32::OLE to interact with Microsoft applications often - I have found ActiveState's VBScriptConverter which ships with their PDK Productivity Tools to be very helpful. This program will convert VB Macros directly to Perl code.

    Another useful tip: After recording a Macro in Excel - press Alt+F11. This will open up Visual Basic where you will be able to view and edit the VBScript generated when you recorded the Macro. You can then convert this to perl yourself using the rules in the Win32::OLE documentation - or paste it into the VBScriptConverter and let it do the work.

    From within Visual Basic - you can also press F2 (or select View->Object Browser) to bring up the object browser. Here you can view the different attributes and methods of the different objects you can interact with.

    I find that when you are unsure of how to do something, this is the easiest way to figure it out is to record the Macro and then look at the VBScript that was generated. This is usually much easier and quicker than piling through documentation.