10 tips for PHP scripts: Access COM functions
Takeaway: Learn how to access Component Object Model (COM) functions with PHP.
By Julie Meloni(2/6/01)
If you're an adventurous soul, and you're running PHP on Windows using the CGI, ISAPI or Apache module version, you can access the COM functions. Now, explaining COM (Microsoft's Component Object Model) is left to Microsoft and very large books. However, for a little taste of what COM can do, here's a common (no pun intended) code snippet.
This code snippet uses PHP to open Microsoft Word in the background, open a new document, type some text, save the document, and close the application:
<?
// create a reference to a new COM component (Word)
$word = new COM("word.application") or die("Can't start Word!");
// print the version of Word that's now in use
echo "Loading Word, v. {$word->Version}<br>";
// set the visibility of the application to 0 (false)
// to open the application in the forefront, use 1 (true)
$word->Visible = 0;
// create a new document in Word
$word->Documents->Add();
// add text to the new document
$word->Selection->TypeText("Testing 1-2-3...");
//save the document in the Windows temp directory
$word->Documents[1]->SaveAs("/Windows/temp/comtest.doc");
// close the connection to the COM component
$word->Quit();
// print another message to the screen
echo "Check for the file...";
?>
Suppose you're running an intranet Web site that has data stored in Microsoft SQL Server, and your users need that data in Excel format. You can have PHP run the necessary SQL queries and format the output, then use COM to open Excel, dump the data stream into it, and save the file on the user's desktop.
Julie Meloni is the technical director at i2i Interactive and is an avowed proponent of Linux and the open source community. A regular contribtor to CNET Builder.com, she has written a few books on PHP and other technologies.
SponsoredWhite Papers, Webcasts, and Downloads
- Microsoft SQL Server and Dell EqualLogic PS Series Solution Brief Dell EqualLogic
- SprintSecure Message Protection Fact Sheet Sprint
- Live Webcast: Enterprise Search Architectures of the Future Google
- New Release - Diskeeper 2008 with InvisiTasking: It's Smart. It's Transparent. It Will Take Your PC from Zero to Sixty--Automatically! Diskeeper
- Sprint IPVoice Connect Fact Sheet Sprint
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET

