I'm trying to make some changes to the text of an existing docx file (simple text replacement for now).
When I save the file, there is no error given, all seems to work. But when I then go ahead and open the file in Word, I get the following error message:
The Open XML file bla.docx cannot be opened because there are problems with the contents or the file name may contain invalid characters (for example, \/).
No error detail available
line 2
Now, I did simplify my code, making no changes at all to the document, just reading it, and then writing it, and I still get that error. So it's not because of something new I added;
$doc = \PhpOffice\PhpWord\IOFactory::load('bla.docx');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($doc, 'Word2007');
$objWriter->save('bla-write.docx');
Any idea what's going on here and how to fix it?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
When I checked the document.xml (from renaming to .zip and unzipping), the written version is only half the size of the original one. So there is possibly an error during reading of the original file. These errors should be outputted. It would be nice also if there was a function such as tidy for word documents.
Hi @gayathriw,
Did you solve the issue? Love to hear the solution cuz I encounter the same issue and still looking for root causes!
Hi @gayathriw,
If you want to replace text from an existing word document just do this:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('path/to/doc/sample.docx'));
$templateProcessor->setValue(array('{{company}}', '{{position}}', '{{name}}', '{{last}}', '{{date}}'), array('Example Company Kenya Limited', 'Web Developer', 'Peter Kitonga', 'Kitonga', '07 Sep 2017'));
$templateProcessor->saveAs($fileName);
Good luck.
@PeterKitonga Thanks, this helped me a lot
My docx exists this problem too, so I have given up to use "load" function
now I'm using my own Class which extends from "TemplateProcessor" to fix my actual business
hope this problem can be fixed
Indeed, load() does not parse the whole docx spec, so parts can be lost. Better to use the TemplateProcessor indeed for simple replacements.
Most helpful comment
Hi @gayathriw,
If you want to replace text from an existing word document just do this:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('path/to/doc/sample.docx'));$templateProcessor->setValue(array('{{company}}', '{{position}}', '{{name}}', '{{last}}', '{{date}}'), array('Example Company Kenya Limited', 'Web Developer', 'Peter Kitonga', 'Kitonga', '07 Sep 2017'));$templateProcessor->saveAs($fileName);Good luck.