Page 1 of 1

problems with COM object for reading MS word.

Posted: Wed Dec 22, 2010 9:42 am
by epezhman
Hi, I'm trying to read a MS word doc, I found these code on a blog which uses COM , but when I run this PHP code I get an exception :

"
com_exception Object ( [message:protected] => Source: Microsoft Word
Description: The requested member of the collection does not exist. [string:Exception:private] => [code:protected] => -2147352567 [file:protected] => C:\wamp\www\PhpProject1\index.php [line:protected] => 31 [trace:Exception:private] => Array (
=> Array ( [file] => C:\wamp\www\PhpProject1\index.php [line] => 31 [function] => unknown ) ) [previous:Exception:private] => )


"

Code: Select all

$filename="cv.doc";
$TXTfilename = $filename . ".txt";

$word = new COM("word.application") or die("Unable to instantiate Word object");
$word->Documents->Open($filename);

// the '2' parameter specifies saving in txt format
$word->Documents[1]->SaveAs($TXTfilename ,2);
$word->Documents[1]->Close(false);
$word->Quit();
$word->Release();
$word = NULL;
unset($word);

$content = file_get_contents($TXTfilename);
unlink($TXTfilename);

I read from here " http://ir2.php.net/manual/en/com.installation.php " that I don't need to install COM but I need to install some COM objects like for MS word doc, but I don't know how to fix it, any idea? thanks in advance :)

Re: problems with COM object for reading MS word.

Posted: Wed Dec 22, 2010 11:18 am
by Weirdan
what is line 31 in your code?

Re: problems with COM object for reading MS word.

Posted: Wed Dec 22, 2010 12:09 pm
by requinix
I'm guessing line 31 is

Code: Select all

$word->Documents[1]->SaveAs($TXTfilename ,2);
If so, the problem is likely that there's only one item in Documents. Considering how array indexes start from zero...

Re: problems with COM object for reading MS word.

Posted: Wed Dec 22, 2010 3:08 pm
by epezhman
tasairis wrote:I'm guessing line 31 is

Code: Select all

$word->Documents[1]->SaveAs($TXTfilename ,2);
If so, the problem is likely that there's only one item in Documents. Considering how array indexes start from zero...
yes It was and I changed it but didn't work.

Re: problems with COM object for reading MS word.

Posted: Wed Dec 22, 2010 3:14 pm
by epezhman
does it mean I don't have the word.application COM?

Re: problems with COM object for reading MS word.

Posted: Wed Dec 22, 2010 11:40 pm
by requinix
If you didn't then the problem would have been earlier in the code.

Look harder at what $word->Documents contains. It probably has a Length or Count property you can quickly check - to make sure there is a document at all. If so, you might need to use the .Item() method that it probably has.
Tip: try the code in VBA and see what happens.