简体   繁体   English

PHP COM('word.application') 函数和方法参数问题

[英]PHP COM('word.application') functions and methods parameters issue

I'm using php and new COM('word.application') to manipulate a .doc file (add and edit fields), I'm on a Windows Server 2012 R2 with apache 2.4.25 x64 and php 7.0.17 x64.我正在使用 php 和 new COM('word.application') 来操作 .doc 文件(添加和编辑字段),我在 Windows Server 2012 R2 上使用 apache 2.4.25 x64 和 php 7.0.17 x64。

So far I made a simple code which work great, but I have an issue when using functions with multiple parameters like this one到目前为止,我制作了一个很好用的简单代码,但是在使用具有多个参数的函数时遇到了问题,例如

$word->Documents[1]->Protect(3, false, 'mypassword', false, false);

I get the following error :我收到以下错误:

[Erreur] Impossible de lancer le connecteur Microsoft Office Word. [错误] 不可能连接到 Microsoft Office Word。 : com_exception: Parameter 4: Le type ne correspond pas : com_exception: 参数4: le type ne 对应pas

Approx translation in english :大概的英文翻译:

[Error] Impossible to laungh the Microsoft Office Word connector : com_exception: Parameter 4: type does not correspond [错误] 无法启动 Microsoft Office Word 连接器:com_exception:参数 4:类型不对应

In VBA code like this it's working在这样的 VBA 代码中它正在工作

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

According to the doc found in VBA根据在 VBA 中找到的文档

而这个:https://msdn.microsof t.com/en-us/library/office/aa220366(v=office.11).aspx t.com/en-us/library/office/aa220366(v=office.11​​).aspx

Parameters 2, 4 and 5 should be booleans, so I don't know why I'm getting this error.参数 2、4 和 5 应该是布尔值,所以我不知道为什么会出现此错误。 If i remove parameters 4 and 5, I get the same error but on parameter 2.如果我删除参数 4 和 5,我会得到相同的错误,但在参数 2 上。

With some searches i looked at the object new VARIANT() but it didn't work too.通过一些搜索,我查看了对象 new VARIANT() 但它也不起作用。

I have also the problem with $this->Documents 1 ->SaveAs()我也有 $this->Documents 1 ->SaveAs() 的问题

$fileName = "D:\\test.doc";
$this->Documents[1]->SaveAs($fileName);

It works.它有效。

According to the doc ( https://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.120).aspx ) I should be able to use a second parameter like this :根据文档( 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);

Like this I have the error "Parameter 0: Le type ne correspond pas" (Type does not correspond).像这样我有错误“参数0:Le type ne对应pas”(类型不对应)。

In VBA it's working differently, maybe I'm doing something wrong ...在 VBA 中它的工作方式不同,也许我做错了什么......

Much thanks if you help me :)如果你帮助我,非常感谢:)

edit : full example code编辑:完整示例代码

<?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>";

The only problem that I see in this code is related to protect function .我在这段代码中看到的唯一问题与protect function First parameter must be VARIANT like this :第一个参数必须是这样的VARIANT

$word->Documents[1]->Protect(
        new VARIANT(3, VT_I4),
        false,
        'Your password',
        false,
        true
  );     

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

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