14

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?

3
  • Word isn't designed to run in a server environment. Even if you solve this problem, you're going to run into many other problems. Commented Sep 28, 2016 at 16:13
  • Is Word installed in your server? Is extension=php_com_dotnet.dll enabled in php.ini? Commented Oct 3, 2016 at 20:32
  • 1
    @espino316 Ms Word is installed in my server && extension=php_com_dotnet.dll is enabled in php.ini. Turns out the problem was Component service(COM) was of 32 bit whereas default php in those servers are of 64 bit. The answerlink for the problem is to register that 32 bit COM service as 64, I found the solution for 2008 server I tried same solution for 2012 server but it won't work. Commented Oct 4, 2016 at 5:23

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried giving Apache service admin rights in services.msc?
It will require that you have a password on the account you're using to run it, though. Hope that helps..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.