Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #2524

Open
wesyah234 opened this issue Dec 7, 2023 · 11 comments

Comments

@wesyah234
Copy link

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.
@Progi1984
Copy link
Member

Which version of PhpWord do you use ?

@wesyah234
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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");

`

@oleibman
Copy link
Contributor

oleibman commented Dec 8, 2023

Try:

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

@wesyah234
Copy link
Author

wesyah234 commented Dec 12, 2023

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...

@oleibman
Copy link
Contributor

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.

@wesyah234
Copy link
Author

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...

@fahrudinyuniwinanto
Copy link

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

@reisraff
Copy link

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.

@oleibman
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants