PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

How do I troubleshoot this error: Word experienced an error trying to open the file. Try these suggestions. * Check the file permissions for the document or drive. * Make sure there is sufficient free memory and disk space. * Open the file with the Text Recovery converter.

Open wesyah234 opened this issue 1 year ago • 11 comments

I'm generating a word document from data in a database, so, for sure, there could be some odd character or something messing things up. For most documents, it works perfectly, just with one set of data, I get this error when trying to open the file. It's a pretty complicated document, so I will probably have to remove content and/or comment code until I find the section of the document causing the issue.... any suggestions?

I've downloaded the doc and "unzipped" it into the underlying files, and it unzips file.

Word experienced an error trying to open the file. Try these suggestions.

  • Check the file permissions for the document or drive.
  • Make sure there is sufficient free memory and disk space.
  • Open the file with the Text Recovery converter.

wesyah234 avatar Dec 07 '23 18:12 wesyah234

Which version of PhpWord do you use ?

Progi1984 avatar Dec 07 '23 20:12 Progi1984

1.1.0 I actually just switched to pulling it in via composer and picking up this up to date version. Before this I was using some really old version from codeplex!

wesyah234 avatar Dec 07 '23 22:12 wesyah234

actually, I just found that it's only having the issue when my text contains an ampersand. I'll do more investigation tomorrow and update here.

wesyah234 avatar Dec 07 '23 22:12 wesyah234

I confirmed this issue and put together a sample snippet to illustrate:

please disregard all the headers I have in there to try to prevent browser caching :)

`

$PHPWord = new \PhpOffice\PhpWord\PhpWord(); $section = $PHPWord->addSection(); // this works: //$section->addText("hello and goodbye"); // this fails: //$section->addText("hello & goodbye"); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); $filename = "test.docx"; header("Content-type: application/vnd.ms-word"); header("Content-disposition: attachment; filename=$filename"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); $objWriter->save("php://output");

`

wesyah234 avatar Dec 08 '23 18:12 wesyah234

Try:

\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);

oleibman avatar Dec 08 '23 23:12 oleibman

Thank you @oleibman that worked... so now, my question is why would this not be "turned on" by default? The old codeplex version of phpword that I was using did not choke on an "ampersand", it was only after I switched to this github version that it began to be an issue... If one were to default to one way or the other, I'd think we should default to "output escaping = true".

My reasoning is this:

If the default is "output escaping = true" then, say someone wants to put &lt; into a document and they want to get the < character. They would then see &lt; in the resulting document and they'd immediately realize they need to find a setting to make it display the < character insteadl

In contrast, if the default is "output escaping = false", and say someone enters an & into their document, they would experience what I just experienced, an error when word tries to open the document. It takes a lot more time to determine this is caused by some random ampersand character in their document, and then to trace it to the output escaping setting.

Edit: I updated my comment because it seems github also interpreted &lt; as < and I see it's necessary to surround this with a "code" block to make it print...

wesyah234 avatar Dec 12 '23 23:12 wesyah234

I believe there were backwards compatibility reasons for this default. I think if the product were being newly introduced today, the default would be different. As it is, I just automatically start all my scripts by changing that setting.

oleibman avatar Dec 13 '23 00:12 oleibman

OK, thanks. Yes, I get it, it would cause numerous problems if a default like this was changed right now so best to leave it as is. Here's hoping others who experience this arrive at this thread without too much effort :)

BTW, I just tried with the old codeplex verison, and interestingly, when I include the & in a document, I get a slightly different error: Word found unreadable content in "filename.docx". Do you want to recover the contents of this document? If you trust the source of this document, click Yes... and when I click Yes, it actually loads and displays the document properly.

With this current github version, it gives the error I have in the title of this issue (Word experienced an error trying to open the file. Try these suggestions. * Check the file permissions for the document or drive. * Make sure there is sufficient free memory and disk space. * Open the file with the Text Recovery converter.), and it refuses to open. So, just interesting... this explains why my users are now complaining when they didn't complain before.

So, if there's anything that might come out of this, perhaps there's a way to make this version behave like the old version and somehow allow the document to open with the warning, instead of outright refusing to open the document. There must be something slightly different about how the document is built...

wesyah234 avatar Dec 13 '23 14:12 wesyah234

for 4 hours I debug my code, that cause of my data include wildchar caracter (&)

fahrudinyuniwinanto avatar Jan 11 '24 04:01 fahrudinyuniwinanto

I had the same problem and the reason of my problem was this:

$table->addRow();
$table->addRow();

Two consecutives addRow. I just removed one, and everything woked fine.

reisraff avatar Jun 13 '24 23:06 reisraff

PR #2516 will eliminate the "two consecutive addRow" problem if it is merged.

oleibman avatar Jun 14 '24 15:06 oleibman