Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't open Word doc with COM in PHP

Tags:

php

ms-word

com

I'm trying to open and read the contents of a Word document with PHO by using the COM function. But whenever i try to read the contents, then it gives me the following error:

Fatal error: Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This command is not available because no document is open.'

I have the following code:

    $word = new \COM ( "word.application" ) or die ( "Could not initialise MS Word object." );
    $word->Documents->Open ( realpath("test.doc" ) );

    // Extract content.
    $content = (string) $word->ActiveDocument->Content;

    echo $content;

    $word->ActiveDocument->Close(false);

    $word->Quit();
    $word = null;
    unset($word);

I'm 100% sure that the Word document exists. I've checked this numerous times. The permissions on the file are set to Full Control. So that can't be it either.


I've also read a comment on php.net. It said that IIS is causing a problem (i have that running). His solution was this:

  • Execute "dcomcnfg"
  • Open Component Services > Computers > My Computer > DCOM Config
  • Search for Microsoft Office Word 97-2003 Document (it will be something like this translated to your language, so take a while and search for it) <-- Stuck at this one, can't find anything called "Microsoft Office..."
  • Right-Click on it and open the properties
  • Choose "Identity" tab
  • Normally this is set to "the launching user". You have to change this to "the interactive user" or a admin user of your choice.
  • Apply these new settings and test your COM application. It should work fine now.

In anyway, i still haven't got this working. Anyone any idea how could possibly fix this error...???

like image 945
w00 Avatar asked Aug 24 '12 07:08

w00


1 Answers

I just found the solution. The steps i talked about:

  • Execute "dcomcnfg"
  • Open Component Services > Computers > My Computer > DCOM Config
  • Search for Microsoft Office Word 97-2003 Document (it will be s....
  • etc..

That works. But like i said, i couldn't find the Microsoft Office Word 97-2003 config. So this is what i've done in order to make it appear in the list.

  1. Start
  2. Run
  3. mmc -32
  4. File
  5. Add Remove Snap-in
  6. Component Services
  7. Add
  8. OK
  9. Console Root
  10. Component Services
  11. Computers
  12. My Computer
  13. DCOM Config
  14. Microsoft Excel Application

So now you should be able to follow the steps from my first post. And PHP should then be able to read the docs without any trouble.

like image 195
w00 Avatar answered Sep 28 '22 08:09

w00