Main goal: create documents (with tables) in Microsoft Word using PHP.
Critical problem encountered: calling:
$document->Range->Find->Execute( ... ); // causes ERROR.
I get information about: line, File, Source: unknown, Description: unknown;
How do I do it (operations in Microsoft Word using PHP):
- I open a document template from a file (success)
- I insert plain text into the document using
->Range->InsertAfter( "whatever text needed to be inserted" ); // (success)
- I define a new Range object:
$range = $document->Range( x, y );
- and convert the Range into a Table (success)
$Table1 = $range->ConverToTable( wdSeparateByTabs, 1, 2, 30, wdTableFormatNone, false, false, false, false, false, false, false, false, false, wdAutoFitWindow, wdWord9ListBehavior );
// with
wdSeparateByTabs = 1
wdTableFormatNone = 0
wdAutoFitWindow = 2
wdWord9ListBehavior = 1
Despite the fact that i pass number_of_rows=1 (2nd parameter), the table looks as expected.
Next step I want, is to make Word find and replace strings:
$rng = $Table1->Range;
$find = $rng->Find;
$find->Execute( "text to find", false, false, false, false, false, null, wdFindContinue, false, "text to be replaced with", wdReplaceAll, null, null, null, null );
Some system info:
- all on a virtual machine Oracle VM VirtualBox 5.2.0
- Windows XP sp3
- Apache 2.2.6
- PHP 5.2.9
- Microsoft Office Word 2002 (10.2627)
Final thoughts and ideas:
- when I comment out the ->Find->Execute( ... ) section - all is done as expected
- I create a table from a plain text with cells separated by a character (tab in my example) thus I can't create a cell with multiple lines (\x0A) this way because Word uses \x0A as a separator too (tested);
- I need to have cells with multiple lines (separated by \x0A or \x0B)
- the idea was to insert a plain text with html's < br > line separator, convert it to a table, and replace '< br >' with \x0A or \x0B when the table is created
- I'm positive that I can achieve needed goal different way (ways)
- I've done a very similar test using C# in VisualStudioExpress2010, but all works as expected, I don't get an ERROR as I do using PHP
What do I do wrong that calling
->Range->Find->Execute(...);
causes an error?
EDIT: I don't use frameworks, just plan Word, and stock PHP
$word = new COM( 'Word.Application' );
$document = $word->Documents->Add( ); //or
$document = $word->Documents->Open( 'template_file.doc' );