BernardV
BernardV

Reputation: 766

Formatting / Styling a table created in Word using COM / PHP

I have been able to create and add a table to my word template, and I have been trawling MSDN to look for a way of controlling this new table's properties via COM, and I'm just not finding anything, MSDN / Google / stackoverflow, if anyone has experience in controlling tables created in this way some assistance would be greatly appreciated!

Example problem, the below creates the a simple two column 1 line table in my word document at a specific point, but it has basically no properties set, I need to add borders etc, atm it's basically invisible unless you highlight the cells in word.

$table1 = $word1->ActiveDocument->Tables->Add($objBookmark1->Range, 1, 2)

I know I need to further extend this statement perhaps with a ref to Styling or something but I cannot find a command that works, keep getting things like:

Uncaught exception 'com_exception' with message 'Unable to lookup `LineStyle': Unknown name.

Upvotes: 1

Views: 346

Answers (1)

BernardV
BernardV

Reputation: 766

Okay I have found a solution, I hope this helps someone in future:

        final class WdLineStyle {

            const wdLineStyleSingle = 1;

            // ... 
        }

//Bookmark found in word document - you want to find this and replace it with the table
        $bookmarkname2 = "TABLE_Budget";
// Wrapped in an 'if' find the correct bookmark and then create a Range and perform the table insertion. STYLING!!!!!
        if ($word1->ActiveDocument->Bookmarks->Exists($bookmarkname2)) {
            //then create a Table and perform the substitution of bookmark with table 
            $ActiveD = $word1->ActiveDocument;
            $objBookmark1 = $ActiveD->Bookmarks($bookmarkname2);
            $Table1 = $ActiveD->Tables->Add($objBookmark1->Range, 3, 4);
            $Table1->Borders->InsideLineStyle = WdLineStyle::wdLineStyleSingle;
            $Table1->Borders->OutsideLineStyle = WdLineStyle::wdLineStyleSingle;
            echo 'Succeafully inserted ' . $bookmarkname2 . "<br />";
        } else {

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

Upvotes: 1

Related Questions