尝试使用 PHP 和 COM 操作 MS Word 书签

发布于 2024-08-04 02:31:51 字数 1028 浏览 1 评论 0原文

我试图使用 PHP 和 COM 使用文档自己的书签来操作提供的 Word 2003 文档中的某些字段,但无论我使用哪种方法,都会出现错误。如果我尝试直接调用书签来替换文本,则会收到错误:无法删除范围。

 function testBkMrkDetails($word, $bookmarkName, $subsTxt) {

try {
   $BkMark = $word->ActiveDocument->Bookmarks($bookmarkName);
   $range = $BkMark->Range;
   if (!$range) {
      echo "Range not created ";
   }
   $range->Text = $subsTxt;
} catch (Exception $e) {
    echo "bookmark failed: " . $e->getMessage() . "\n";
}
}

我使用错误消息在 Google 上进行了搜索,并寻找 PHP 和 COM 教程,这似乎建议我查看 FormFields,所以我写了以下内容:

 function testFormFlds ($word, $bookmarkName, $subsTxt) {

try {
    $formField = $word->Selection->FormFields($bookmarkName);
    if (!$formField) {
        die("Form failed : " . $bookmarkName . " not found\n");
    }

    $formField->Result($subsTxt);
} catch (Exception $e){
    echo "FormField failed: " . $e->getMessage();
}

}

但是,这一直告诉我,所请求的集合成员不存在,所以我'我假设我没有正确调用字段名称(它是来自文档的 taen)。如果您能提供一些解决此问题并了解更多底层技术的建议,我将不胜感激。谢谢,伊恩

I'm trying to manipulate some fields in a supplied Word 2003 document using the document's own bookmarks using PHP and COM but am getting an error which ever method that I use. If I try to call the Bookmarks directly to substitute the text I get an error: The range cannot be deleted.

 function testBkMrkDetails($word, $bookmarkName, $subsTxt) {

try {
   $BkMark = $word->ActiveDocument->Bookmarks($bookmarkName);
   $range = $BkMark->Range;
   if (!$range) {
      echo "Range not created ";
   }
   $range->Text = $subsTxt;
} catch (Exception $e) {
    echo "bookmark failed: " . $e->getMessage() . "\n";
}
}

I've searched on Google using the error message and loking for PHP and COM tutorials which appeared to suggest that I look at FormFields so I wrote the following:

 function testFormFlds ($word, $bookmarkName, $subsTxt) {

try {
    $formField = $word->Selection->FormFields($bookmarkName);
    if (!$formField) {
        die("Form failed : " . $bookmarkName . " not found\n");
    }

    $formField->Result($subsTxt);
} catch (Exception $e){
    echo "FormField failed: " . $e->getMessage();
}

}

However this keeps telling me that the requested member of the collection does not exists so I'm assuming that I have not called the feild name correctly (it was taen from the document). I would be grateful for some pointers towards solving this and learning more about the underlying technology. Thanks, Iain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文