使用COM打开Word

发布于 2024-08-26 23:06:45 字数 865 浏览 5 评论 0原文

我实际上正在尝试从 http://php.net/manual/en 找到的一些代码/class.com.php

  <?php
    // starting word
    $word = new COM("word.application") or die("Unable to instantiate Word");
    echo "Loaded Word, version {$word->Version}\n";

    //bring it to front
    $word->Visible = 1;

    //open an empty document
    $word->Documents->Add();

    //do some weird stuff
    $word->Selection->TypeText("This is a test...");
    $word->Documents[1]->SaveAs("Useless test.doc");


    //closing word
    $word->Quit();

    //free the object
    $word = null;
    ?> 

但这似乎不起作用。我使用的是 Word 2007,我得到以下信息:

已加载 Word,版本 12.0 致命错误:第 14 行 C:\xampp\htdocs\final\testq.php 中调用未定义的方法变体::SaveAs()

任何人都可以解决这个问题吗?是因为我用的是Word 2007吗?

I am actually trying some codes i found from http://php.net/manual/en/class.com.php

  <?php
    // starting word
    $word = new COM("word.application") or die("Unable to instantiate Word");
    echo "Loaded Word, version {$word->Version}\n";

    //bring it to front
    $word->Visible = 1;

    //open an empty document
    $word->Documents->Add();

    //do some weird stuff
    $word->Selection->TypeText("This is a test...");
    $word->Documents[1]->SaveAs("Useless test.doc");


    //closing word
    $word->Quit();

    //free the object
    $word = null;
    ?> 

But this does not seem to work. I am using Word 2007 and i get the following:

Loaded Word, version 12.0
Fatal error: Call to undefined method variant::SaveAs() in C:\xampp\htdocs\final\testq.php on line 14

Can anyone solve this problem? Is it because i am using Word 2007?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不放开 2024-09-02 23:06:45

Documents 对象是一个 Collection 对象,而不是一个数组。尝试:

$word->Documents(1)->SaveAs("Useless test.doc");

或者

$word->ActiveDocument->SaveAs("Useless test.doc");

The Documents object is a Collection object, not an array. Try:

$word->Documents(1)->SaveAs("Useless test.doc");

Or

$word->ActiveDocument->SaveAs("Useless test.doc");
我不在是我 2024-09-02 23:06:45

您的示例对我来说运行良好,在 Window 7 上使用 Word 2003 和 Word 2007。因此,我认为问题可能是 Word 安装/配置不正确。要进行故障排除,请执行以下操作:

  • 修复 Word 安装
  • 确保 Word 已以运行脚本的同一用户身份至少启动一次
  • 禁用所有加载项
  • 转至 %APPDATA%\Microsoft\Templates\ 并重命名 Normal.dot(x) 文件
  • 确保您确实有权将文件保存到指定位置,尝试使用绝对路径

Your sample runs fine for me, both with Word 2003 and Word 2007 on Window 7. Therefore I assume that the problem might be an incorrectly installed/configured Word. For troubleshooting do the following:

  • repair the Word installation
  • make sure Word has been started at least once as the same user your script runs under
  • disable all add-ins
  • go to %APPDATA%\Microsoft\Templates\ and rename the Normal.dot(x) file
  • make sure that you actually have permissions to save files to the specified location, try with an absolute path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文