简体   繁体   English

在创建表格并将其插入MS Word文档/模板PHP时遇到问题

[英]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. 我试图在MS Word文档中创建新表并将其插入,但是遇到了麻烦,我们将不胜感激。 I'm encountering the following error: 我遇到以下错误:

'Unable to lookup `InsertTable': Unknown name. '无法查找`InsertTable':未知名称。 '

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.';
        }

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 您没有提及正在使用的Word PHP库,因此无法给出具体示例,但是InsertTable很可能是函数而不是参数

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 ) 如果您使用的是第三方库(例如PHPDocX),则可以在其网站上找到文档( 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 如果您正在使用COM对象,则可以尝试搜索MSDN( http://msdn.microsoft.com/zh-cn/library/office/bb726434 ( v=office.12 ) .aspx ),因为该对象将是记录您正在使用的Word版本

(Although if you -are- using the COM directly then you could try something like: (尽管如果您直接使用COM,则可以尝试执行以下操作:

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

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM