Anything's possible with a little help from OLE (and an hour to kill reading Excel docs;-)
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Const "Microsoft Excel";
use Win32::OLE::Const "Microsoft Visual Basic for Applications Extensi
+bility";
my $xls = Win32::OLE->new('Excel.Application');
$xls->{Visible} = -1;
my $wb = $xls->Workbooks->Add();
my $mod = $wb->VBProject->VBComponents->Add(vbextFileTypeModule);
$mod->CodeModule->AddFromString ( <<MODTEXT);
Sub Message
dim s as string
s = "Hello, World"
MsgBox s
End Sub
MODTEXT
my $sheet = $xls->Sheets('Sheet1');
my $button = $sheet->Buttons->Add(71.25, 18.75, 88.5, 57);
$button->{OnAction} = 'Message';
Notes:
- Error checking is left as an exercise.
- I tested this with Excel 97.
- VBProject objects are horribly documented in the Excel helpfiles. I ended up using the object browser from ActiveState to put it all together.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.