|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
因为函数实在是太多了,慢慢的你就会知道,知道有这个函数就可以。window|word COM Functions in PHP4 (Windows)
Alain M. Samoun
Introduction
The built-in COM functionality of PHP4 is quite attractive for some of us programming in the win32 environment. So far, there is not much documentation on the subject. This short article will explain how to use COM in real PHP4 programming with three examples using MS office 2000 Word and Excel programs and the Adobe Distiller program. The COM technology has been developed by Microsoft for several years, under different names. As far as this article is concerned, the words OLE, OLE Automation, ActiveX and COM are all the same: They designate an encapsulated piece of code (the Object) that performs some functions for a windows application. PHP4 COM connects to the object (Instantiate the object) and uses its methods and properties.
If you want to reproduce the following examples, here is my configuration:
Windows 98 - MS Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI
COM tags in PHP4
Lets start with the specific information to use the COM functions with PHP4. To instantiate a component, one needs the "new" operator and the "OLE programmatic identifiers" of the object:
<?php
$instance = new COM("$identifier");
?>
Since COM is a reserved class name in PHP4, it passes the object's identifier to the constructor. Now that we have instantiated the component, we can easily reach its methods and properties, using the OOP class. For example:
<?php
$instance->[Object]->[method1]->[method2]->..->[property];
?>
It's that simple!
There are two tag functions for PHP4 COM that are used when the OOP construct doesn't work. (In the case of PHP syntax problems, with the names and values of properties with invalid characters, like dot or parenthesis):
<?php
bool com_set(class com_object, string property name, string property_value);
mixed com_get(class com_object, string property_name);
?>
Finally, PHP4 also supports DCOM to create an instance of an object on a remote computer:
<?php
$Instance = new COM(string "Component name", string "remote_server_address");
?>
Note: there is a DCOM directive to set in the PHP configuration. PHP developers may add DCOM support for Unix in the future. That's all, there are no other functions to remember!
Identifiers, methods and properties.
identifiers are strings like:
For MS Word: "Word.Application" or "Word.Application.9"
MS Excel: "Excel.Application" or "Excel.Sheet"
ADOBE Acrobat: "Exch.application" or "PdfDistiller.PdfDistiller"
As the last identifier name indicates, it is not always easy to know the right name for the object. If you do not have access to a VBA doc, you can look at the windows registry (Start - Run regedit) and look in the HKEY_CLASSES_ROOT folder: Scan down until the end of the extensions list, you will then reach the Application names. The COM Identifiers available in your machine, are the folders with the CLSID subfolders.
The application program should document its COM methods and properties. In the case of Office 2000, start the application, then open the visual basic editor with the <ALT+F11> short cut key and select the Objects Editor <F2>. Enter a name of method or properties for the application's library. Then, select a class or member and right click on the member name in the next window below. You will get the description for the class or member by selecting help. You can also consult MSDN. An example for Excel is: http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm
Using PHP4 COM functions with MS Word
Now, we have all we need to start with the first code example:
<?php
#*********************************************************
# This example, slightly modified from the Zend site,
# will open an instance of word with a new
# document with the name "Useless test.doc" and the line:
# "This is a test2..." typed inside.
#*********************************************************
#Instantiate the Word component.
$word = new COM("word.application") or die("Unable to instantiate Word");
#Get and print its version
print "Loaded Word, version {$word->Version}<BR>";
#Another way to get the version using com_get
$testversion = com_get($word->application,version);
print "Version using Com_get(): $testversion <BR>";
#Make it visible in a window
$word->Visible = 1;
#Open a new document
$word->Documents->Add();
#Write something
$word->Selection->TypeText("This is a test...");
#Now save the document
$word->Documents[1]->SaveAs("Useless test.doc");
#Comment next line if you want to see the word document,
#then close word manually
$word->Quit();
#Comment if you want to see the word document, then close
?>
If you study this example for a few minutes using the OLE documentation that comes with Word, you will learn practically all you need to write your own program.
我先解释一下我的学习思路。 |
|