Jump to content

Word COM Object throwing exception


Thierry
Go to solution Solved by Thierry,

Recommended Posts

I'm working on reading the content of a bunch of Word files into a text field in MySQL.

My setup runs on Windows and has Word 2007 installed so I figured I could use the .COM object.

I quickly found a promising bit of code, but whatever I do I keep getting the following error:

 

Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Word<br/><b>Description:</b> This command is not available because no document is open.' in C:\Apache\htdocs\XXX\public_html\misc\docReader.php:10 Stack trace: #0 C:\Apache\htdocs\XXX\public_html\misc\docReader.php(10): unknown() #1 {main} thrown in C:\Apache\htdocs\XXX\public_html\misc\docReader.php on line 10

 

It doesn't crash on the line where it opens the file, but where I want to use the ActiveDocument to read the content. If I deliberatly pass a bad path to a file, it gives a different error, so it does seem to be able to find the file.

The code is as followed.

 

$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);

 

Is there something about Word 2007 that I'm missing maybe? I've had no problems with .COM on Word 2000/2003, although I haven't used it in quite a while.

 

I've tried Documents->Add, Document[0], Document[1], Document["test.doc"], all to no avail.

 

Any ideas?

Link to comment
Share on other sites

  • Solution

$word->version returns version 12.0, don't know if that's any help.

 

On php.net I thought I found the answer

 

If you're having permission problems like not being able to opening or saving a document and you're getting errors like:

 

- This command is not available because no document is open

or

- Command failed

 

try this (if you're running IIS):

 

- 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)

- 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.

 

However, after trying this and using both interactive user or admin user, it still gives the same error.

Don't know if I have to restart something other than Apache before these take effect though.

 

Link to comment
Share on other sites

  • 3 years later...

$word->version returns version 12.0, don't know if that's any help.

 

On php.net I thought I found the answer

 

 

 

However, after trying this and using both interactive user or admin user, it still gives the same error.

Don't know if I have to restart something other than Apache before these take effect though.

thanks,  It solved the problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.