BernardV
BernardV

Reputation: 766

Encountering problems creating and inserting a table into an MS word doc/template PHP

I'm attempting to create and insert a new table into an MS word document, and I'm having trouble with this, any help would be much appreciated. I'm encountering the following error:

'Unable to lookup `InsertTable': Unknown name. '

So obviously my syntax for accessing the function is incorrect but I'm having trouble finding a resource that can succinctly tell me how to do this.

Code sample:

//1. Instanciate Word 
        $word1 = new COM("word.application") or die("Unable to instantiate Word");
        echo "Loaded Word, version {$word1->Version}\n";
//2. specify the MS Word template document (with Bookmarks inside) 
        $template_file = "C:\PHP/TEST.docx";
//3. open the template document 
        $word1->Documents->Open($template_file);

// get the bookmark and create a new MS Word Range (to enable text substitution)        
    $bookmarkname2 = "TABLE_Budget";

        if ($word1->ActiveDocument->Bookmarks->Exists($bookmarkname2)) {
            //then create a Table and perform the substitution of bookmark with table 
            $objBookmark1 = $word1->ActiveDocument->Bookmarks($bookmarkname2);
            $table1 = $word1->ActiveDocument->Tables->Add($word1->Selection->Range, 1, 2); //creates table with 2 columns
            //now substitute the bookmark with actual value 
            $objBookmark1->InsertTable = $table1;
        } else {

            echo 'Problem found on ' . $bookmarkname2 . 'insert.';
        }

Upvotes: 1

Views: 443

Answers (1)

Tim
Tim

Reputation: 491

You don't mention what Word PHP library you are using, so it's impossible to give a specific example, however it is highly likely that InsertTable is a function and not a parameter

You would use it something like

$objBookmark1->InsertTable($tableData);

If you are using a 3rd party library such as PHPDocX then you will find documentation on their website (http://www.phpdocx.com/documentation/api-documentation)

If you are using the COM object then you can try searching the MSDN (http://msdn.microsoft.com/en-us/library/office/bb726434(v=office.12).aspx), as the object will be documented for the version of Word you are using

(Although if you -are- using the COM directly then you could try something like:

//add a table at the bookmark position
$table1 = $word1->ActiveDocument->Tables->Add($objBookmark1->Range, 1, 2);

)

Upvotes: 1

Related Questions