Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I did find it, and updated it somewhat.
This has been tested against Excel 2002 sp3, on XP
Note that this script will leave dangling Excel processes in memory if given the chance, and will not end if you manually close Excel while it is running.
As usual with these things, ActiveState's OLE browser comes in handy, as well as the documentation for Excel's Object model. In particular for identifying the arguments your event handler(s) can potentially receive.
use strict; use sigtrap qw(die INT); use Win32::OLE qw(in EVENTS); use Win32::OLE::Variant; $| = 1; # Without this, you end up registering the same event multiple times.. +. # However, check the docs, as there is a performance penalty... Win32::OLE->Option(_Unique => 1); # Excel 10 "Dispatch" interfaces for Win32::OLE->WithEvents # Application: AppEvents # Sheets: DocEvents # Charts: ChartEvents # Workbooks: WorkbookEvents # OLE: OLEObjectEvents sub kill_excel { my $self = shift; $self->Quit; } END { my $Count = Win32::OLE->EnumAllObjects(sub { my $Object = shift; my $Class = Win32::OLE->QueryObjectType($Object); printf "# Object=%s Class=%s\n", $Object, $Class; }); print $Count,$/; } my $xl_app = Win32::OLE->new('Excel.Application', \&kill_excel); $xl_app->{Visible} = 1; my %wkb; my %sheets; sub get_ole_type { return Win32::OLE->QueryObjectType(shift); } sub app_event_handler { my ($obj, $event, @args) = @_; #print "app_event_handler: ", get_ole_type($obj), " ($obj->{Name}) : + Event: $event",$/; my $new_item = $args[0]; if ($event eq'NewWorkbook'|| $event eq 'WorkbookActivate' ){ print "app_event_handler: ", get_ole_type($obj), " ($obj->{Name}) +: Event: <$event>",$/; print $new_item->{BuiltinDocumentProperties}->{"Creation Date"}; print $new_item, ":",join("<->", keys %wkb), $/; unless ($wkb{$new_item}){ my $w = $new_item; print "Registering event handler for Workbook: $w->{Name}",$/; Win32::OLE->WithEvents($w, \&wb_event_handler, 'WorkbookEvents') +; $wkb{$w} = $new_item; for my $s (in $w->Worksheets()) { $sheets{$s} = $s unless $sheets{$s}; print "Registering event handler for sheet $s->{Name} in book $s-> +{Parent}->{Name}",$/; Win32::OLE->WithEvents($s, \&sheet_event_handler, 'DocEvents'); } } } } Win32::OLE->WithEvents($xl_app, \&app_event_handler, 'AppEvents'); sub sheet_event_handler { my ($obj, $event, @args) = @_; print "sheet_event_handler: ",get_ole_type($obj), " ($obj->{Name}) : + Event: <$event>",$/; if ($event eq 'SelectionChange' || $event eq 'Change') { my $range = $args[0]; print "$event received with " . get_ole_type($range) . " as argume +nt",$/; print <<EOF; The target range has: $range->{Columns}->{Count} column(s) and $range- +>{Rows}->{Count} row(s) It spans cell(s) $range->{Address} EOF } if ($event eq 'Change') { for my $cell ( in $args[0]->{Cells}) { print "Cell $cell->{Address} contains: $cell->{Value}",$/; } } } sub wb_event_handler { my ($obj, $event, @args) = @_; print "wb_event_handler: ",get_ole_type($obj), " ($obj->{Name}) : Ev +ent: <$event>",$/; } while (1) { $xl_app->SpinMessageLoop(); }
HTH
--
Olivier

In reply to Re^2: Excel events handling by olivierp
in thread Excel events handling by esr

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found