【问题标题】:Uncaught exception 'com_exception' with message '<b> Source: </ b> Microsoft Word <b> Description: </ b> Command failed'未捕获的异常 'com_exception' 带有消息 '<b> 来源:</b> Microsoft Word <b> 描述:</b> 命令失败'
【发布时间】:2016-01-02 19:50:43
【问题描述】:

运行脚本后我的脚本出现问题 未捕获 带有消息“来源:Microsoft Word 描述:命令失败”的异常“com_exception”

并显示警报: 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);
?>

【问题讨论】:

  • 你试过用 try..catch 包围你的代码吗?把try{放在一切之前,把}catch(Exception $e){ echo $e; }放在一切之后。
  • 这个comment 可能就是你要找的。​​span>
  • subhanallah 这就是答案谢谢@FirstOne

标签: php com ms-word office-interop


【解决方案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; 替换为实际的错误处理。

【讨论】:

  • 抱歉@Firstone 看来我必须提前获得 15 的声望分数才能添加您的答案,如果可以的话,我会在您的答案中给您分数,干杯 :)
  • 嗯,你快到了,如果@FirstOne +1 是你的帖子和你的一个 cmets,你会超过 15 个代表点
猜你喜欢
  • 2015-10-25
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-02-25
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多