未被捕获的例外&#39; com_exception&#39;消息&#39; <b>来源:<! - b - > Microsoft Word <b>描述:<! - b - >命令失败&#39;

时间:2015-10-06 02:22:21

标签: php com ms-word office-interop

我运行脚本后出现 Uncaught,我的脚本出现问题 例外&#39; com_exception&#39;有消息&#39;来源:Microsoft Word说明:命令失败&#39;

并显示提醒: http://imgur.com/HrDYrah

这是我的代码:

<?php
   $word = new COM("Word.Application") or die ("Could not initialise Object.");
   // set it to 1 to see the MS Word window (the actual opening of the document)
   $word->Visible = 0;
   // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc"
   $word->DisplayAlerts = 0;
   // open the word 2007-2013 document 
   $word->Documents->Open('C:\xampp\htdocs\Open_Office\test_4sk.docx');
   // save it as word 2003
   $word->ActiveDocument->SaveAs('C:\xampp\htdocs\Open_Office\newdocument.doc');
   // convert word 2007-2013 to PDF
   $word->ActiveDocument->ExportAsFixedFormat("C:\\xampp\htdocs\Open_Office\Main_PDF.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
   // quit the Word process
   $word->Quit(false);
   // clean up
   unset($word);
?>

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

<?php
   try{
       $word = new COM("Word.Application") or die ("Could not initialise Object.");
       // set it to 1 to see the MS Word window (the actual opening of the document)
       $word->Visible = 0;
       // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc"
       $word->DisplayAlerts = 0;
       // open the word 2007-2013 document 
       $word->Documents->Open('C:\xampp\htdocs\Open_Office\test_4sk.docx');
       // save it as word 2003
       $word->ActiveDocument->SaveAs('C:\xampp\htdocs\Open_Office\newdocument.doc');
       // convert word 2007-2013 to PDF
       $word->ActiveDocument->ExportAsFixedFormat("C:\\xampp\htdocs\Open_Office\Main_PDF.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
       // quit the Word process
       $word->Quit(false);
       // clean up
       unset($word);
   }catch(Exception $e){
       echo $e;
   }
?>

这样它就不会停止代码的执行,你可以看到它以防它发生。请注意,这里理想的做法是将echo $e;替换为实际的错误处理。