$file='C:/a.doc';
$saveas=__DIR__.'/tmp/a.html';
$word=new COM('word.application');
$document=$word->Documents->Open($file);
$document->SaveAs($saveas,8);
$word->Quit();
$word=null;
echo file_get_contents($saveas);
@unlink($saveas);
It works fine in PHP CLI mode(php -f "com_server.php"), but when I run it through web(windows/apache http://localhost/com_server.php), an error occurred:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Server execution failed' in D:\www\wwwroot\com_server.php:16 Stack trace: #0 D:\www\wwwroot\co开发者_如何转开发m_server.php(16): com->com('word.applicatio...') #1 {main} thrown in D:\www\wwwroot\com_server.php on line 16
I've also tried to wrap the code in shell_exec, but same error as well.
Any suggestions?
I'm guessing that line 16 is the one from SaveAs()
at:
$document=$word->Documents->Open($file);
$document->SaveAs($saveas,8);
As you told on the comment for
Sometimes the $word->Documents->Open()
gives that error; try with something like $word->ActiveDocument
if you have previously opened it, or try:
$word->Documents->Add()
$word->ActiveDocument->open()
And then, try the save statement. Looks like you haven't been able to open it correctly. I haven't tested this in a while, so give it a shot. Also, remember to don't leave parenthesis on sentences such as print
(but curly braces), and to null your object when you have finished.
If neither of this works, check this page for an extensive explanation of the expression.SaveAs2
method for Word. Maybe it will help. Good luck.
精彩评论