【发布时间】:2017-11-01 12:40:52
【问题描述】:
我正在使用 php 和 new COM('word.application') 来操作 .doc 文件(添加和编辑字段),我使用的是带有 apache 2.4.25 x64 和 php 7.0 的 Windows Server 2012 R2。 17 x64。
到目前为止,我编写了一个简单的代码,效果很好,但是在使用像这样的具有多个参数的函数时遇到了问题
$word->Documents[1]->Protect(3, false, 'mypassword', false, false);
我收到以下错误:
[错误] 不可能 de lancer le connecteur Microsoft Office Word。 : com_exception: 参数4: Le type ne对应pas
大概的英文翻译:
[错误] 无法启动 Microsoft Office Word 连接器:com_exception:参数 4:类型不对应
在这样的 VBA 代码中它可以工作
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
根据 VBA 中找到的文档
t.com/en-us/library/office/aa220366(v=office.11).aspx
参数 2、4 和 5 应该是布尔值,所以我不知道为什么会出现这个错误。如果我删除参数 4 和 5,我会得到相同的错误,但在参数 2 上。
通过一些搜索,我查看了对象 new VARIANT(),但它也不起作用。
我也有 $this->Documents1->SaveAs() 的问题
$fileName = "D:\\test.doc";
$this->Documents[1]->SaveAs($fileName);
有效。
根据文档 (https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.120).aspx),我应该能够像这样使用第二个参数:
$fileName = "D:\\test.doc";
$fileFormat = 0;
$this->Documents[1]->SaveAs($fileName, $fileFormat);
像这样我有错误“参数0:Le type ne对应pas”(类型不对应)。
在 VBA 中它的工作方式不同,也许我做错了什么......
如果你能帮助我,非常感谢:)
编辑:完整示例代码
<?php
$word = new COM("word.application") or die("Cannot create Word object");
echo "com object created<br>";
$word->Visible = false;
$word->WindowState = 2;
$word->DisplayAlerts = false;
$word->Documents->Open("D:\\test.doc");
echo "document loaded<br>";
$import = $word->Documents[1]->VBProject->VBComponents->Import("D:\\fr.mac");
$import->name = "Macrofr";
$myModule = $word->Documents[1]->VBProject->VBComponents->Import("D:\\test1.mac");
$myModule->name = "MacroInit";
$myModule2 = $word->Documents[1]->VBProject->VBComponents->Import("D:\\test2.mac");
$myModule2->name = "MacroFonction";
$myModule3 = $word->Documents[1]->VBProject->VBComponents->Import("D:\\test3.frm");
$myModule3->name = "MacroProgression";
$word->Documents[1]->Protect(3,false,'motdepasse',false,false); // (type de protection,noReset,mdp)
$word->Documents[1]->SaveAs("D:\\doc_with_macro.doc", 0);
$word->Documents[1]->Close();
echo "closing doc<br>";
$word->Quit();
$word = null;
echo "end<br>";
【问题讨论】:
-
我已经编辑了我的帖子并改变了一点问题,欢迎任何帮助:)
-
请发布更多您的代码块,而不是几行 sn-ps。我们需要看看你是如何初始化 COM 对象的。
-
嗨,我已经编辑了我的第一篇文章并添加了完整的代码。我找到了一些东西......我正在使用 apache 2.4.25 x64 vc14 + php 7.0.17 x64 vc14 + office 2010 x64,我已经用相同的版本更改了 apache 和 php,但 x86 ......现在一切正常...所以我的问题解决了...但我很好奇为什么它不适用于 x64 版本