Excel VB Controls
krazken
created: 2006-05-02 14:27:08
Does anyone know an easy way to gain access to VB controls that are in a Excel spreadsheet? I have a spreadsheet I am trying to parse, and they have checkboxes all over the place and I am trying to figure out if the checkbox is checked or not. thanks
Re: Excel VB Controls
created: 2006-05-02 15:37:44
Greetings krazken,

I have not used Excel in a while. Are these check boxes actually VB controls? If so take a look at How to convert a VBA macro to Perl from the ActiveState docs. You could create a macro to query these values and the convert it to perl using the information linked to in the above document. Also the Where do I find documentation for the object models? may be of use should you wish to drive Excel via Win32::OLE.

Hope this helps.

Martin
Re: Excel VB Controls
created: 2006-05-02 23:39:01
You'll want to check out the "Shapes" method in the Worksheet object using Marto's Where do I find documentation for the object models? reference.

Each form control is a "Shape", and its values can be manipulated through the OLEFormat object.

The following sample code should put you on the right track. It will create a worksheet with a check box, check it, and then uncheck it. (If you're using something other than Excel 2003, you'll need to update the "Microsoft Excel 11.0 Object Library" to the correct version)

use strict;
use Win32::OLE;
use Win32::OLE::Const("Microsoft Excel 11.0 Object Library");

# Load Excel objects
my $xl = Win32::OLE->new('Excel.Application');
$xl->{Visible} = 1;
my $book = $xl->Workbooks->Add;
my $sheet = $book->Worksheets->Add;

# Add a check box to manipulate
$sheet->Shapes->AddFormControl(xlCheckBox, 0, 0, 250, 250);

# Get the name of our checkbox
my $chkBoxName = $sheet->Shapes(1)->Name;

# Check the check box
$sheet->Shapes($chkBoxName)->OLEFormat->Object->{Value} = 1;
sleep 2;
 
# Uncheck the check box
$sheet->Shapes($chkBoxName)->OLEFormat->Object->{Value} = 0;

perlmonks.org content © perlmonks.org and krazken, marto, Unanimous Monk

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03