tpj.html

来自「perl教程」· HTML 代码 · 共 676 行 · 第 1/5 页

HTML
676
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../../displayToc.js"></script>
<script language="JavaScript" src="../../../tocParas.js"></script>
<script language="JavaScript" src="../../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../../scineplex.css">
<title>#10 - Win32::OLE by Jan Dubois</title>
<link rel="stylesheet" href="../../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',3);</script>
<h1><a>#10 - Win32::OLE by Jan Dubois</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#introduction">INTRODUCTION</a></li>
	<li><a href="#the_ole_mindset">THE OLE MINDSET</a></li>
	<li><a href="#working_with_win32__ole">WORKING WITH WIN32::OLE</a></li>
	<ul>

		<li><a href="#the_first_step__creating_an_ole_server_object">THE FIRST STEP: CREATING AN OLE SERVER OBJECT</a></li>
		<li><a href="#method_calls">METHOD CALLS</a></li>
		<li><a href="#properties">PROPERTIES</a></li>
		<li><a href="#sample_application">SAMPLE APPLICATION</a></li>
		<li><a href="#downloading_a_web_page_with_lwp">DOWNLOADING A WEB PAGE WITH LWP</a></li>
		<li><a href="#microsoft_access">MICROSOFT ACCESS</a></li>
		<li><a href="#microsoft_excel">MICROSOFT EXCEL</a></li>
		<li><a href="#activex_data_objects">ACTIVEX DATA OBJECTS</a></li>
		<li><a href="#lotus_notes">LOTUS NOTES</a></li>
	</ul>

	<li><a href="#variants">VARIANTS</a></li>
	<li><a href="#further_information">FURTHER INFORMATION</a></li>
	<ul>

		<li><a href="#documentation_and_example_code">DOCUMENTATION AND EXAMPLE CODE</a></li>
		<li><a href="#ole_automation_on_other_platforms">OLE AUTOMATION ON OTHER PLATFORMS</a></li>
	</ul>

	<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>The Perl Journal #10 - Win32::OLE by Jan Dubois</p>
<p>
</p>
<hr />
<h1><a name="introduction">INTRODUCTION</a></h1>
<p>Suppose you're composing a document with Microsoft Word. You want to
include an Excel spreadsheet. You could save the spreadsheet in some
image format that Word can understand, and import it into your
document. But if the spreadsheet changes, your document will be out of
date.</p>
<p>Microsoft's OLE (Object Linking and Embedding, pronounced &quot;olay&quot;) lets
one program use objects from another. In the above scenario, the
spreadsheet is the object. As long as Excel makes that spreadsheet
available as an OLE object, and Word knows to treat it like one, your
document will always be current.</p>
<p>You can control OLE objects from Perl with the Win32::OLE module, and
that's what this article is about. First, I'll show you how to &quot;think
OLE,&quot; which mostly involves a lot of jargon. Next, I'll show you the
mechanics involved in using Win32::OLE. Then we'll go through a Perl
program that uses OLE to manipulate Microsoft Excel, Microsoft Access,
and Lotus Notes. Finally, I'll talk about Variants, an internal OLE
data type.</p>
<p>
</p>
<hr />
<h1><a name="the_ole_mindset">THE OLE MINDSET</a></h1>
<p>When an application makes an OLE object available for other
applications to use, that's called OLE <em>automation</em>. The program
using the object is called the <em>controller</em>, and the application
providing the object is called the <em>server</em>. OLE automation is guided
by the OLE Component Object Model (COM) which specifies how those
objects must behave if they are to be used by other processes and
machines.</p>
<p>There are two different types of OLE automation servers. <em>In-process</em>
servers are implemented as dynamic link libraries (DLLs) and run in
the same process space as the controller. <em>Out-of-process</em> servers
are more interesting; they are standalone executables that exist as
separate processes - possibly on a different computer.</p>
<p>The Win32::OLE module lets your Perl program act as an OLE
controller. It allows Perl to be used in place of other languages like
Visual Basic or Java to control OLE objects. This makes all OLE
automation servers immediately available as Perl modules.</p>
<p>Don't confuse ActiveState OLE with Win32::OLE. ActiveState OLE is
completely different, although future builds of ActiveState Perl (500
and up) will work with Win32::OLE.</p>
<p>Objects can expose OLE methods, properties, and events to the outside
world. Methods are functions that the controller can call to make the
object do something; properties describe the state of the object; and
events let the controller know about external events affecting the
object, such as the user clicking on a button. Since events involve
asynchronous communication with their objects, they require either
threads or an event loop. They are not yet supported by the Win32::OLE
module, and for the same reason ActiveX controls (OCXs) are currently
unsupported as well.</p>
<p>
</p>
<hr />
<h1><a name="working_with_win32__ole">WORKING WITH WIN32::OLE</a></h1>
<p>The Win32::OLE module doesn't let your Perl program create OLE
objects. What it does do is let your Perl program act like a remote
control for other applications-it lets your program be an OLE
controller. You can take an OLE object from another application
(Access, Notes, Excel, or anything else that speaks OLE) and invoke
its methods or manipulate its properties.</p>
<p>
</p>
<h2><a name="the_first_step__creating_an_ole_server_object">THE FIRST STEP: CREATING AN OLE SERVER OBJECT</a></h2>
<p>First, we need to create a Perl object to represent the OLE
server. This is a weird idea; what it amounts to is that if we want to
control OLE objects produced by, say, Excel, we have to create a Perl
object that represents Excel. So even though our program is an OLE
controller, it'll contain objects that represent OLE servers.</p>
<p>You can create a new OLE <em>server object</em> with <code>Win32::OLE-&gt;new</code>.
This takes a program ID (a human readable string like
<code>'Speech.VoiceText'</code>) and returns a server object:</p>
<pre>
  <span class="keyword">my</span> <span class="variable">$server</span> <span class="operator">=</span> <span class="variable">Win32::OLE</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="string">'Excel.Application'</span><span class="operator">,</span> <span class="string">'Quit'</span><span class="operator">);</span>
</pre>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?