开发者

Using Office Word to read doc files with PHP

开发者 https://www.devze.com 2023-02-03 11:40 出处:网络
I am trying to use PHP with word.application to read a file. I开发者_JAVA百科t simply will not open the file.It\'s echoing the right version.

I am trying to use PHP with word.application to read a file. I开发者_JAVA百科t simply will not open the file. It's echoing the right version.

$w = new COM("word.application") or die("Is office installed?");
echo 'Loaded Word, version ' . $w->Version . '<br>'; 
$w->Visible = false;

$w->Documents->Open(realpath('test.docx'));

$content = (string) $w->ActiveDocument->Content;

echo $content;

$w->Quit();
$w->Release();
$w = null;

I get the error:

Uncaught exception 'com_exception' with message 'Source: Microsoft Word
Description: This command is not available because no document is open.' 

It feels like it's some kind of permission problem. I tried to put the path of the test.docx besides using realpath and that did not help. Also tried to put it in the root of my C drive. I am using Windows 7 Professional and Microsoft Office 2007.


Documents->Open returns a document if all is ok. Probably, the document does not exists (path incorrect), or you haven't got the rights to open it from PHP. Store the result in $var, check if it has an appropriate value (probably not isset, null or false if not), and use $var->Content to read the content.


  1. Try doing a file_exists on said file/path.
  2. If that works, try file_get_contents and see if you can read it.

If that all works - then it's not a problem with permissions/etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号