Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

COM Functions in PHP4 (Windows)
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:
  • 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:

[ Next Page ]


Comments:
how to parse word file having images, tableskashif07/07/08 07:35
PHP + Com + Wordsebastian reikowski07/12/07 10:39
Com and PHPsubangini05/31/07 07:56
Using Excel COM without MS Excel installedJames Gallardo11/24/05 10:28
converting xls file into csv using phpraj10/21/05 01:27
RE: slow down executing com Aradhana09/28/05 08:11
RE: slow down execute com mjqtreble09/27/05 11:24
$word-> Invoke Error PAULO EDUARDO MAIA07/30/05 18:52
com php/worddinanath07/25/05 06:23
Concurrent Access Problem using PHP and COMUruloki07/08/05 05:00
Apache + W2K3 Server + COM + PHP= Apache failJames H06/29/05 12:37
Excel FitToPagesWide propertyHolger06/15/05 08:15
slow down execute com Iwan 05/13/05 07:22
com problem in xpIwan 05/13/05 06:40
RE: Porting VB to PHPAlex05/03/05 16:48
RE: Mysql to xlsJoe G.04/30/05 17:29
RE: Mysql to xlspepienk03/02/05 22:23
COM and Word Page NumberWeston01/27/05 23:33
RE: Warning: Type library value....dennis01/09/05 20:54
PHP WordRodney Norman12/28/04 00:39
Inserting jpg into Word DocumentPJ Newberry12/10/04 13:23
How do run an excel macro with PHPiman khabazian12/08/04 01:59
ParagraphFormat->AligmentAnna11/29/04 01:43
RE: error while creating an instance of EXCEL Amjad Damisi11/25/04 04:12
RE: error while creating an instance of EXCEL Amjad Damisi11/25/04 04:09
RE: HELP NEEDED : COM & PHP4 on Win2k / IIS5Soha Abouseif11/24/04 05:13
Problem with COM/MSWord under IIS5Soha Abouseif11/24/04 05:05
RE: Com objects & variables passed by referenceFeroz Zahid08/24/04 01:30
RE: Text formatting -Word Templatessonic04/01/04 21:01
RE: International characters in ExcelSLON03/17/04 11:04
com php/wordGringo10/31/03 08:10
PROBLEMS UNDER WIN2K and XP...alain M samoun08/29/03 13:27
RE: Using CDONTStravis johnson03/10/03 21:25
RE: Method callsEdinsson Ariza01/02/03 16:25
Cannot print (to file) from ExcelGaetano Giunta12/17/02 06:44
RE: Help !! pdf conversionGaetano Giunta12/17/02 06:38
Problem with COMAlex11/05/02 10:43
PHP COMbijurama10/31/02 09:09
introduce an optional parameter in a functionajithlal10/31/02 06:58
Porting VB to PHPAlex Dow10/30/02 21:55
unbold the words ...jiayi10/29/02 21:57
Warning: Type library value....Andrew Chance10/17/02 11:18
NULL pointer exception Tha Chief10/04/02 02:03
COM PHP crash or malfunction?John Adair09/18/02 17:59
error while creating an instance of EXCEL Muhammad Qasim09/13/02 04:04
RE: Using com_get() or com_propget()Ben09/11/02 15:38
RE: Displaying ApplicationBen09/11/02 15:36
can't save or close excel worksheetSimon09/10/02 17:21
Cannot instantiate non-existent class: com ??acidbox09/04/02 15:51
PHP & COM & MS WORDJOSE2309/03/02 03:34
Method callsNick K. Aghazarian08/31/02 23:43
RE: Excel, COM and PHPOliver08/27/02 06:01
RE: Plese checkMilind Pingale08/24/02 08:31
RE: COM and PHP - Start on ClientLorenz Wiedemar08/23/02 11:47
RE: ADVICE: COM Problems in win2k and NTavenger08/19/02 13:32
RE: COM on XP !?Nicholas Clark08/05/02 21:17
Displaying ApplicationLance07/31/02 10:33
RE: notepad and php extentionsRoy07/22/02 08:00
Problem with Com ObjectSuper Bill07/16/02 02:59
COM and PHP - Start on ClientSeb07/16/02 02:30
please help me alainCaillard Youri (fr)07/10/02 08:32
Sample table (COM)ctb12307/09/02 05:32
Word Doc Questionctb12307/08/02 23:49
Properties with "PropPutRef" attributeLaurent06/25/02 04:44
Help !! pdf conversionKumar06/21/02 15:25
RE: Word FormFieldsalain06/12/02 15:37
Word FormFieldsTrent06/12/02 13:19
RE: identifiers for winampalain06/06/02 20:13
RE: identifiers for winamprym06/06/02 10:50
COM on XP !?Sebastian06/05/02 01:19
RE: PHP/COM problemmalain06/03/02 20:08
PHP/COM problemmfiga06/02/02 14:49
RE: PHP / COM: Printing ProblemLou Popovsky06/02/02 01:38
identifiers for winampAlvaro Torres05/29/02 16:13
RE: PHP / COM: Printing Problemalain05/22/02 23:44
PHP and Crystal ReportsSanti05/20/02 07:55
RE: Outlook, DCOM and PhpDave Forrest05/20/02 03:44
PHP / COM: Printing ProblemMichael05/15/02 09:34
help i need document com with php on windowsomax05/10/02 01:03
RE: Outlook, DCOM and Phpalain05/08/02 14:10
RE: HELP NEEDED : COM & PHP4 on Win2k / IIS5alain05/08/02 14:00
RE: COM and PHP and Wordalain05/08/02 13:56
Passing parameter by reference to DLLIlya05/07/02 23:11
Outlook, DCOM and PhpThomas05/02/02 17:42
COM and PHP and WordRaoul04/30/02 12:08
RE: HELP NEEDED : COM & PHP4 on Win2k / IIS5Oscar04/30/02 10:28
RE: HELP NEEDED : COM & PHP4 on Win2k / IIS5Oscar04/30/02 10:01
COM with PHPmickael04/24/02 11:04
HELP NEEDED : COM & PHP4 on Win2k / IIS5D. Simmons04/11/02 16:05
Saving Outlook contact Items with PHPPeter Mulder03/30/02 12:46
Save CSV file under EXCELbatataw03/29/02 13:08
RE: Invalid ProgID, GUID string, or MonikerJeff03/29/02 12:05
RE: Invalid ProgID, GUID string, or MonikerBogomil03/29/02 09:50
RE: Outlook tasksBogomil03/29/02 09:46
Invalid ProgID, GUID string, or MonikerKen Johanson03/27/02 18:06
COM In IIS Isapi Problemhongky03/25/02 01:15
RE: hiding table gridlines in word?alain03/23/02 20:15
hiding table gridlines in word?digihoo03/22/02 10:17
RE: Excel, COM and PHPalain03/21/02 12:53
Excel, COM and PHPWill03/19/02 05:42
RE: PHP +COM +ADSI??alain03/14/02 00:11
objects as session variablesmariano03/13/02 16:45
PHP +COM +ADSI??Steven Holloway03/12/02 09:29
RE: Fatal error: NULL pointer exception alain03/11/02 17:02
Fatal error: NULL pointer exception Dario03/08/02 09:15
Excel.application vs Excel.sheetAlex Madon03/07/02 12:50
RE: Need Help [COM]alain03/01/02 21:05
Need Help [COM]baabu02/27/02 01:18
RE: Text formatting -Word TemplatesJason Hoover02/25/02 17:32
RE: COM Configuration in php.ini? alain02/25/02 00:13
'Unable to obtain IDispatch ...' ErrorsJoeyG02/21/02 20:08
COM Configuration in php.ini? JoeyG02/21/02 17:08
RE: COM and serial portsalain02/20/02 12:00
Com objects & variables passed by referencebartek02/20/02 10:09
COM and serial portsetherface02/16/02 04:28
PHP + COM + MapPointFerriol02/14/02 13:03
RE: Outlook tasksalain02/13/02 14:00
Outlook tasksJordi02/12/02 12:59
RE: COM & MS Wordalain02/11/02 18:20
RE: win OS functionsalain02/11/02 18:09
ASP COM operation to PHP: NevermindRon02/11/02 13:41
translating ASP to PHPRon02/11/02 13:16
RE: COM & MS WordMike Molotov Minski02/08/02 06:45
RE: Text formatting -Word TemplatesMike Molotov Minski02/08/02 06:38
win OS functionsdigihoo02/07/02 18:11
RE: open a word documentpascall02/07/02 05:32
RE: How to skip optional parameters?alain02/06/02 19:22
RE: How to skip optional parameters?Alexei Krus02/06/02 08:59
RE: instantiating objects on the local machine?alain02/04/02 21:58
RE: How to skip optional parameters?alain02/04/02 21:48
How to skip optional parameters?Alexei Krus02/04/02 13:51
instantiating objects on the local machine?digihoo02/01/02 10:41
RE: ADVICE: COM Problems in win2k and NTdigihoo02/01/02 10:01
COM + IIS 5digihoo02/01/02 09:34
Excel not visibleJoe Mainolfi01/30/02 13:39
Problems with comJoe Mainolfi01/30/02 10:52
RE: MY SOLUTION: How to find the right objectalain01/26/02 11:17
RE: call to method with parameter failedalain01/26/02 11:07
MY SOLUTION: How to find the right objecttimtim01/25/02 05:07
RE: Fatal error in creating of COMtimtim01/25/02 05:05
Fatal error in creating of COMCalvin01/24/02 01:51
RE: call to method with parameter failedalain01/23/02 11:47
RE: attach Powerpoint in Wordfile via COMalain01/23/02 11:39
call to method with parameter failedtimtim01/22/02 11:42
attach Powerpoint in Wordfile via COMtimtim01/21/02 07:00
RE: XLS to Mysqlalain01/15/02 12:41
RE: PDF+comalain01/15/02 12:39
RE: Plese checkalain01/15/02 12:32
RE: PHP and Crystal ReportsGeorge Neill01/10/02 13:15
XLS to Mysqlumesh kumar01/10/02 02:30
PDF+comumesh kumar01/09/02 05:54
Plese checkBlue zou01/08/02 22:48
IIS+COMChristophe01/03/02 18:56
RE: email formatalain12/30/01 21:00
email formatshams 12/24/01 03:52
RE: can't write word filealain12/17/01 14:26
PHP + MS Word errcircle12/15/01 01:40
can't write word filecircle12/14/01 10:13
RE: VT_BYREF, com function modif. typealain12/09/01 12:42
VT_BYREF, com function modif. typesostene12/06/01 15:24
RE: NULL pointer exception for new COMKevin Aebig12/06/01 13:00
RE: COM & MS Wordpytournier11/28/01 10:32
open a word documentpyt11/28/01 10:23
PHP and Crystal ReportsChris11/27/01 11:48
php + adoFerenc11/22/01 15:30
RE: ADVICE: COM Problems in win2k and NTJohn Asendorf11/21/01 19:12
RE: controlling winampNils-Fredrik G. Kaland11/20/01 10:20
RE: Events?alain11/17/01 14:55
RE: Events?Anikin11/15/01 13:37
RE: strings chopped in halfalain11/14/01 14:20
strings chopped in halfGiuseppe D'Ambrosio11/08/01 05:13
COM-PHP FAQalain11/07/01 18:01
RE: ADVICE: COM Problems in win2k and NTArve Bersvendsen11/06/01 01:13
RE: controlling winampwtracz11/03/01 04:58
Copy and Paste with Excel COMFrancesco Marsan10/31/01 04:27
RE: ADVICE: COM Problems in win2k and NTFrancesco Marsan10/30/01 11:54
RE: ADVICE: COM Problems in win2k and NTFrancesco Marsan10/30/01 05:43
RE: NULL pointer exception for new COMRudi Swatoch10/29/01 05:05
RE: PHP and OutlookChristophe10/21/01 16:51
RE: PHP and OutlookChristophe10/18/01 15:26
RE: COM Excel printalain10/16/01 20:15
COM Excel printCOM Excel print10/11/01 04:25
RE: notepad and php extentionsSini10/09/01 06:49
RE: PHP + MS Word+OLEalain10/06/01 18:41
RE: Cannot instantiate non-existent classalain10/06/01 18:32
PHP + MS Word+OLEBora Çetiner10/03/01 08:47
ADVICE: COM Problems in win2k and NTalain09/30/01 15:00
Create Table in WordArnaud09/25/01 12:04
RE: NT Login with PHP?Arnaud09/25/01 11:59
RE: Cannot instantiate non-existent classRich09/12/01 23:09
activex form in php?vrspace09/12/01 16:13
Error in Word examplealain09/10/01 14:06
PHP + MS Word + Fonts ??tom knudsen09/10/01 04:28
Removing Excel from memory after useJeff Waldock09/08/01 13:39
Using com_get() or com_propget()Kelvin09/06/01 22:48
Creating COM components in PHP?Allyn Craig08/28/01 21:39
Trapping COM errorsneil08/17/01 05:27
Can I store COM object in sessionVlad08/17/01 04:48
OLEVariantRuben08/16/01 09:54
RE: ADO -> smth elseLukasz Szmit07/27/01 22:44
RE: ActiveX controls?David Morris07/27/01 08:25
ADO + PHP£ukasz Szmit07/18/01 23:16
controlling winampBram Bruurs07/18/01 04:46
Unsupported variant type: 8194 (0x2002) Luiz Fernando07/16/01 12:22
NULL pointer exception for new COMTony07/11/01 05:53
DLL´s using under PHPsnark07/11/01 04:23
DLL´s using under PHPsnark07/11/01 04:23
Can't invoke any method on a COM instancePeter07/10/01 02:01
apache crash Smito07/02/01 13:19
Apache CrashSmito07/02/01 13:09
NT Login with PHP?Thomas Nielsen06/30/01 16:00
php and excelben06/22/01 08:13
I can't use the function com_load ben06/22/01 06:23
RE: ActiveX controls?Mike Hagan06/07/01 07:25
PHP and OutlookAlihsan06/01/01 06:10
RE: notepad and php extentionsalain05/30/01 14:20
RE: Error in PHP4TS.DLLpeter05/28/01 15:57
COM & MS WordArnis05/26/01 04:34
RE: Access ViolationNeil J. McLeish05/25/01 04:18
RE: Error in PHP4TS.DLLalain05/23/01 17:09
RE: notepad and php extentionschris05/22/01 08:45
ADO -> smth elseMichael Yevdokimov05/21/01 07:33
RE: notepad and php extentionsEric Eilertson05/20/01 17:27
how to get a retrun value of a tring ?liyi05/15/01 23:51
RE: Error in PHP4TS.DLLChristophe05/15/01 10:25
notepad and php extentionstonio05/14/01 14:01
RE: php + DelphiXander05/03/01 17:06
php + DelphiAndras Bajza04/25/01 02:49
PHP4 and MTS componentsGlenn04/23/01 10:05
COM troublejbv04/20/01 12:59
ActiveX controls?Mike04/19/01 08:29
RE: chart/ how to make a chart in excelmac04/14/01 10:10
Chartmac04/07/01 11:16
excell or word loaded on nt servertriet04/05/01 09:31
International characters in Exceltrash04/04/01 08:47
COM is not a Class ?dirk folkerts04/03/01 13:56
What about scurity? NTserver+OfficeNicola04/02/01 05:35
RE: Convertingalain04/01/01 13:23
RE: COM is not a Class ?alain03/29/01 11:11
ConvertingHakan03/28/01 15:30
COM is not a Class ?Patrice03/18/01 17:02
multiple parameters from a COMJoerg03/01/01 01:06
RE: Unable to obtain IDispatch interface (whaalain02/28/01 09:59
RE: Unable to obtain IDispatch interface (whabhooyman02/27/01 03:24
RE: GetObject ??alain02/24/01 09:41
RE: COM and DCOM on NT for VeraStreamalain02/24/01 09:38
RE: COM and DCOM on NT for VeraStreamHeine Broers02/22/01 08:07
MSXML 3.0 examplePatricia DiBenedetto02/19/01 14:39
National specific charactersdeepx02/11/01 07:11
COM and DCOM on NTdeepx02/11/01 06:58
GetObject ??Tim Knip02/08/01 06:40
RE: Unable to obtain IDispatch interfacealain02/01/01 14:08
RE: Access is deniedalain02/01/01 14:05
RE: NT vs. Windows 98Yoni02/01/01 13:09
RE: Unable to obtain IDispatch interfaceCengiz Soyukan02/01/01 07:03
Access is deniedCengiz Soyukan02/01/01 05:55
RE: Access ViolationChris01/10/01 03:59
RE: Access Violationalain01/09/01 16:07
Access ViolationChris01/08/01 04:25
RE: PHP + MS WordSean Fell12/20/00 07:10
RE: Com functions with win2kyusuf12/16/00 21:51
RE: PHP + MS Wordalain12/16/00 14:06
Type errorJulien12/11/00 07:11
NT vs. Windows 98Weihong Hendricks12/07/00 12:00
Com functions with win2kLinda Pedersen11/23/00 02:50
Events?Rik11/21/00 05:29
PHP + MS WordSean Fell11/20/00 12:17
Unable to obtain IDispatch interfaceDominick Vansevenant11/14/00 10:36
french charactersmarc 11/13/00 05:15
RE: PHP + MS Wordalain11/07/00 02:34
RE: PHP + MS Worddeg11/03/00 14:39
RE: PHP + MS Wordmitesh11/02/00 07:30
Text formattingkevin hickey10/27/00 11:06
RE: How to change the directory for word dockevin hickey10/27/00 11:03
COM objects with more than one interfaceJose10/27/00 07:04
Invoke() failed, wrong variabletypeRaschied10/26/00 09:11
COM+ ?Cledar10/24/00 21:22
RE: COM Functions in PHP4 (Windows)Bhuvnesh10/22/00 23:40
PHP & MsWordPaloi / bRS10/18/00 05:58
RE: How to change the directory for word docBen Munoz10/16/00 19:57
Com Collections?Leonard Ooi10/16/00 19:21
How to change the directory for word docwinnie10/15/00 01:51
RE: Com Erroralain10/12/00 01:40
RE: COM and PHPalain10/12/00 01:35
COM and PHPBill Gates10/11/00 12:24
Com ErrorMike Grover10/11/00 11:50
RE: problem using method argumentsalain10/11/00 02:04
RE: COM Functions in PHP4 (Windows)Cengiz Soyukan10/10/00 08:28
problem using method argumentslepionka stephane10/10/00 04:17
RE: PHP + MS Wordjustin10/09/00 22:10
RE: using MTS ...alain10/09/00 18:28
RE: COM Functions in PHP4 (Windows)alain10/09/00 17:45
RE: PHP + MS Wordalain10/09/00 17:40
RE: Outlook?Urs Gehrig10/09/00 16:50
Create Com object with selected interfaceLlorenç10/09/00 09:35
PHP + MS WordCengiz Soyukan10/09/00 04:29
COM Functions in PHP4 (Windows)Cengiz Soyukan10/09/00 04:19
using MTS ...Stephane Baudet10/06/00 12:29
RE: Using CDONTSalain10/06/00 01:32
RE: Using CDONTSalain10/05/00 23:19
Outlook?Neil Moomey10/05/00 21:36
RE: Using CDONTSMukul Sabharwal10/05/00 05:56
RE: On Linux ...alain10/04/00 13:47
RE: Cannot instantiate non-existent classalain10/04/00 13:45
RE: Using CDONTSalain10/04/00 13:41
Using CDONTSMukul Sabharwal10/04/00 06:12
Cannot instantiate non-existent classTum10/04/00 05:36
On Linux ...Danny Appaiah10/04/00 03:46
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.