Home >Backend Development >PHP Tutorial >8 Tips for PHP Scripting 6PHP and COM_PHP Tutorial
PHP and COM
If you are an adventurer and you are running PHP on a Windows system using a CGI, ISAPI or Apache module version, then you can also get the system's COM capabilities. Now, the job of explaining COM (Microsoft's Component Object Model) is left to Microsoft and those big books. However, there's nothing wrong with knowing a little bit about COM, and here's a common (no pun intended, very common) little snippet of code.
This small snippet of code uses PHP to launch Microsoft Word in the background, open a new file, type some text, save the file and then close the application:
// create a reference to a new COM component (Word)
$word = new COM("word.application") or die("Cant start Word!");
// print the version of Word thats now in use
echo "Loading Word , v. {$word->Version}
";
// 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