epezhman Posted December 22, 2010 Share Posted December 22, 2010 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 ( [0] => Array ( [file] => C:\wamp\www\PhpProject1\index.php [line] => 31 [function] => unknown ) ) [previous:Exception:private] => ) " here is the code $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 Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 22, 2010 Share Posted December 22, 2010 Unfortunately those error messages are a bit vague. When you're debugging - can you pinpoint at which line of code is throwing the error? Quote Link to comment Share on other sites More sharing options...
epezhman Posted December 22, 2010 Author Share Posted December 22, 2010 here it is : exception 'com_exception' with message 'Source: Microsoft Word Description: The requested member of the collection does not exist.' in C:\wamp\www\PhpProject1\index.php:29 Stack trace: #0 C:\wamp\www\PhpProject1\index.php(29): unknown() #1 {main} these line I guess : $word->Documents[1]->SaveAs($TXTfilename ,2); $word->Documents[1]->Close(false); Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted December 22, 2010 Share Posted December 22, 2010 Should it be Documents[0] ? Isn't there only one item in the array? Quote Link to comment Share on other sites More sharing options...
epezhman Posted December 22, 2010 Author Share Posted December 22, 2010 I changed it yo zero, but still doesn't work. Quote Link to comment Share on other sites More sharing options...
epezhman Posted December 22, 2010 Author Share Posted December 22, 2010 does it mean I don't have the word.application COM? Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted December 22, 2010 Share Posted December 22, 2010 Either that, or it can't find the file your trying to open. Quote Link to comment Share on other sites More sharing options...
therob1 Posted December 29, 2010 Share Posted December 29, 2010 i used similar coding to, but to add in some extra content to the word document using bookmarks. However, I commented out one line and it appeared to fix the problem for me - maybe it will work for you? $word->Quit(); //$word->Release(); $word = null; when i checked task manager afterwards I could see that word had exited correctly and ive not fully built a solution around what I intend to use this for so i dont know if there are any implications for doing this - maybe someone else will expand upon this? Quote Link to comment Share on other sites More sharing options...
epezhman Posted December 29, 2010 Author Share Posted December 29, 2010 i used similar coding to, but to add in some extra content to the word document using bookmarks. However, I commented out one line and it appeared to fix the problem for me - maybe it will work for you? $word->Quit(); //$word->Release(); $word = null; when i checked task manager afterwards I could see that word had exited correctly and ive not fully built a solution around what I intend to use this for so i dont know if there are any implications for doing this - maybe someone else will expand upon this? thanks for the reply .I tried it but it didn't work. I think it could't open the .doc file at all. when I write echo count($word->Documents); it just print 1, which means It doesn't open the doc file. I guess that create the COM application but can't open the the file. I tried it with different version of MS word and even windows OS but the same result. any more ideas? thanks Quote Link to comment Share on other sites More sharing options...
lastkarrde Posted December 29, 2010 Share Posted December 29, 2010 Are you providing the full path to the file? (C:\\Users\\name\\file.doc sort of thing) Quote Link to comment Share on other sites More sharing options...
epezhman Posted December 29, 2010 Author Share Posted December 29, 2010 Are you providing the full path to the file? (C:\\Users\\name\\file.doc sort of thing) thanks for mentioning that but didn't working neither. I put the file in project folder so I guess that wouldn't be a problem. Quote Link to comment Share on other sites More sharing options...
therob1 Posted December 30, 2010 Share Posted December 30, 2010 i did read somewhere that its default location for word was somewhere weird, i maybe wrong, but it might be worth double checking the path, i hard coded my path to avoid any doubt during testing and i used forward slashes instead "c:/mydoc.doc" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.