Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xampp PHP Create COM Object Fatal Error without any info

I'm running into a problem with my php application. I'm building up a service application which should be connected to our ERP ( selectline ).

But I can't get to create a COM Object. This code:

<?PHP
error_reporting(E_ALL);
session_start();
date_default_timezone_set('Europe/Zurich');
echo time();
$obj = new COM("fd6c8b29-e936-4a61-8da6-b0c12ad3ba00") or die("Unable to instantiate Word");
echo "Loaded Word, version {$word->Version}\n";
?>

Returns me:

Fatal error: in C:\xampp\htdocs\com.php on line 21

I got two environments:

Server 2008 R2 + Xampp v3.1.0 |PHP 5.4.7

Server 2012 R2 + Xampp v3.1.0 | PHP 5.4.7

I had to add the php_com_dotnet.dll in the php.ini because I had COM class not found before.

Due to the fact I'm complete new to COM I have no idea where to search for the failure.

May you guys can help me out.

Thanks in advance

I changed my code a bit:

try {
$obj = new COM("word.application") or die("Unable to instantiate Word");
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}

Now I get:

Failed to create COM object `word.application': Invalid Syntax

I don't get what is wrong. According to this article http://www.php.net/manual/en/class.com.php there is no need for additional parameters, right?

like image 485
da.eXecutoR Avatar asked Jan 28 '14 12:01

da.eXecutoR


1 Answers

Make sure to set the correct COM permission to "This User".

  • Run: "dcomcnfg"
  • Expand: Component Services > Computers > My Computer > DCOM Config.
  • Select: "Microsoft Word 97-2003 Document"
  • Right-Click on it and open the properties
  • Click the Identity tab.
  • Select "This User" and type the username and password for the (Administrator) user.
  • Apply these new settings and test your COM application. It should work fine now.

I hope I save lots and lots of hours of headaches to some of you :)

PHP Setup

  • Enable the COM extension in php.ini

extension=php_com_dotnet.dll

  • Restart Apache

Usage

<?php

try {
    $word = new COM("word.application");
} catch (Exception $ex) {
    echo $ex->getMessage();
    exit;
}

$word->Screenupdating = true;
$word->WindowState = 2;
$word->Visible = 0;
$word->CheckLanguage = false;
$word->Options->Overtype = false;
$word->Options->SaveInterval = 0;
$word->Assistant->Visible = false;
$word->DisplayAlerts = false;

// Do fancy stuff...

// Close word
$word->Quit();
$word = null;
like image 114
odan Avatar answered Sep 22 '22 13:09

odan