【问题标题】:Encountering problems creating and inserting a table into an MS word doc/template PHP在 MS Word 文档/模板 PHP 中创建和插入表格时遇到问题
【发布时间】:2014-03-21 00:52:50
【问题描述】:

我正在尝试创建一个新表格并将其插入到 MS Word 文档中,但我遇到了麻烦,任何帮助将不胜感激。我遇到以下错误:

'无法查找 `InsertTable':未知名称。 '

很明显,我访问该函数的语法不正确,但我无法找到可以简洁地告诉我如何执行此操作的资源。

代码示例:

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

【问题讨论】:

    标签: php object com ms-word


    【解决方案1】:

    您没有提及您使用的是什么 Word PHP 库,因此无法给出具体示例,但很有可能InsertTable 是函数而不是参数

    你会使用类似的东西

    $objBookmark1->InsertTable($tableData);
    

    如果您使用的是第三方库,例如 PHPDocX,那么您可以在他们的网站 (http://www.phpdocx.com/documentation/api-documentation) 上找到文档

    如果您使用的是 COM 对象,那么您可以尝试搜索 MSDN (http://msdn.microsoft.com/en-us/library/office/bb726434(v=office.12).aspx),因为该对象将针对您使用的 Word 版本进行记录

    (虽然如果你 - 正在 - 直接使用 COM,那么你可以尝试类似的方法:

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

    )

    【讨论】:

    • 有道理,很抱歉没有完全添加那个细节 - (只添加了 COM 标记)我现在正在更新我的帖子,感谢您迄今为止的反馈,现在查看它!
    • 感谢您的回复,我已经能够添加一个表格,唯一的问题是该表格在文档中没有边框/样式,但基本上不可见,您需要突出显示它才能看到它。
    • 可以使用$ table1添加边框之类的东西,比如$ table1->Borders->InsideLineStyle = <value>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多