Win32-OLE
view release on metacpan or search on metacpan
lib/Win32/OLE/TPJ.pod view on Meta::CPAN
context and the OLE error message in a string context, just like
Perl's C<$!> variable.
Now Mary can add today's data:
# Add new record to table
use Win32::OLE::Variant;
$Win32::OLE::Variant::LCID = $Win32::OLE::LCID;
my $Fields = [qw(Day Open High Low Close)];
my $Values = [Variant(VT_DATE, $Day),
$Open, $High, $Low, $Close];
Mary uses the Win32::OLE::Variant module to store C<$Day> as a date
instead of a mere string. She wants to make sure that it's stored as
an American-style date, so in the third line shown here she sets the
locale ID of the Win32::OLE::Variant module to match the Win32::OLE
module. (C<$Win32::OLE::LCID> had been set earlier to English, since
that's what the Chicago Board of Trade uses.)
{
local $Win32::OLE::Warn = 0;
$Recordset->AddNew($Fields, $Values);
}
# Replace existing record
if (Win32::OLE->LastError) {
$Recordset->CancelUpdate;
$Recordset->Close;
$Recordset->Open(<<"SQL", $Connection, adOpenDynamic);
SELECT * FROM $Contract
WHERE Day = #$Day#
SQL
$Recordset->Update($Fields, $Values);
}
$Recordset->Close;
$Connection->Close;
The program expects to be able to add a new record to the table. It
fails if a record for this date already exists, because the Day field
is the primary index and therefore must be unique. If an error occurs,
the update operation started by AddNew() must first be cancelled with
C<< $Recordset->CancelUpdate >>; otherwise the record set won't close.
=head2 LOTUS NOTES
Now Mary can use Lotus Notes to mail updates to all her customers
interested in the T-bond data. (Lotus Notes doesn't provide its
constants in the OLE type library, so Mary had to determine them by
playing around with LotusScript.) The actual task is quite simple: A
Notes session must be started, the mail database must be opened and
the mail message must be created. The body of the message is created
as a rich text field, which lets her mix formatted text with object
attachments.
In her program, Mary extracts the email addresses from her customer
database and sends separate message to each. Here, we've simplified it
somewhat.
sub EMBED_ATTACHMENT {1454;} # from LotusScript
my $Notes = Win32::OLE->new('Notes.NotesSession');
my $Database = $Notes->GetDatabase('', '');
$Database->OpenMail;
my $Document = $Database->CreateDocument;
$Document->{Form} = 'Memo';
$Document->{SendTo} = ['Jon Orwant >orwant@tpj.com>',
'Jan Dubois >jan.dubois@ibm.net>'];
$Document->{Subject} = "US T-Bonds Chart for $Day";
my $Body = $Document->CreateRichtextItem('Body');
$Body->AppendText(<<"EOT");
I\'ve attached the latest US T-Bond data and chart for $Day.
The daily statistics were:
\tOpen\t$Open
\tHigh\t$High
\tLow\t$Low
\tClose\t$Close
Kind regards,
Mary
EOT
$Body->EmbedObject(EMBED_ATTACHMENT, '', $Filename);
$Document->Send(0);
=head1 VARIANTS
In this final section, I'll talk about Variants, which are the data
types that you use to talk to OLE objects. We talked about this line
earlier:
my $Values = [Variant(VT_DATE, $Day),
$Open, $High, $Low, $Close];
Here, the Variant() function creates a Variant object, of type C<VT_DATE>
and with the value C<$Day>. Variants are similar in many ways to Perl
scalars. Arguments to OLE methods are transparently converted from
their internal Perl representation to Variants and back again by the
Win32::OLE module.
OLE automation uses a generic C<VARIANT> data type to pass
parameters. This data type contains type information in addition to
the actual data value. Only the following data types are valid for OLE
automation:
B<Data Type Meaning>
VT_EMPTY Not specified
VT_NULL Null
VT_I2 2 byte signed integer
VT_I4 4 byte signed integer
VT_R4 4 byte real
VT_R8 8 byte real
VT_CY Currency
VT_DATE Date
VT_BSTR Unicode string
VT_DISPATCH OLE automation interface
VT_ERROR Error
VT_BOOL Boolean
VT_VARIANT (only valid with VT_BYREF)
VT_UNKNOWN Generic COM interface
VT_UI1 Unsigned character
The following two flags can also be used:
VT_ARRAY Array of values
VT_BYREF Pass by reference (instead of by value)
B<The Perl to Variant transformation.> The following conversions are
performed automatically whenever a Perl value must be translated into
a Variant:
Perl value Variant
Integer values VT_I4
Real values VT_R8
Strings VT_BSTR
undef VT_ERROR (DISP_E_PARAMNOTFOUND)
Array reference VT_VARIANT | VT_ARRAY
Win32::OLE object VT_DISPATCH
Win32::OLE::Variant object Type of the Variant object
What if your Perl value is a list of lists? Those can be irregularly
shaped in Perl; that is, the subsidiary lists needn't have the same
( run in 0.960 second using v1.01-cache-2.11-cpan-2c0d6866c4f )