COM(';word.docent';)以只读方式打开,而文件由其他人打开


COM('word.document') open in read only whilst file is opened by other

我正在使用php,并试图摆弄COM对象来读取Word文件。我找不到任何文件。

我想做的是以只读方式打开一个文件,这样我就不会在主机上弹出"文件正在使用"的弹出窗口。

如何通过COM告诉word以只读方式打开文件?我正在尝试使用变体,但我得到以下错误:

Parameter 0: Type mismatch. #0 C:'xampp'htdocs'test.php(17): variant->Open('''remote'test'test.doc', false, true, false, Object(variant), Object(variant), Object(variant), Object(variant), Object(variant), Object(variant), Object(variant), true, true, Object(variant), Object(variant), true) #1 {main}

这是我使用的代码

$word = new COM("word.application") or die("Unable to instantiate application object");
$wordDocument = new COM("word.document") or die("Unable to instantiate document object");
$MISSING =  new VARIANT();
$word->Visible = 0;
$DocumentPath = "''remote'test'test'alamo.doc";
$HTMLPath = "";
try {
$wordDocument = $word->Documents->Open("''exit-dc'eeb'test'alamo.doc"/* FileName */, false/* ConfirmConversions */, true/* ReadOnly */, 
                                    false/* AddToRecentFiles */, $MISSING/* PasswordDocument */, $MISSING/* PasswordTemplate */, 
                                    $MISSING/* Revert */, $MISSING/* WritePasswordDocument */, $MISSING/* WritePasswordTemplate */, 
                                    $MISSING/* Format */,$MISSING/* Format */, $MISSING/* Encoding */, true/* Visible */, true/* OpenConflictDocument */, 
                                    $MISSING/* OpenAndRepair */, $MISSING/* DocumentDirection */, true/* NoEncodingDialog */);
$HTMLPath = substr_replace($DocumentPath, 'html', -3, 3);
if($wordDocument !== null) {
    $wordDocument->SaveAs($HTMLPath, 3);//3 = text, I know.
}
}
catch(Exception $ex){
    echo $ex->getMessage() . $ex->getTraceAsString();
}
$wordDocument = null;
$word->Quit();
$word = null;

我想要什么?打开带有只读标志的文件。我只想从中读取。我知道我可以通过只提供文件名来做到这一点,这很有效,但我需要它来处理多个读取同一文件的实例。

无论出于何种意图和目的,这都应该起作用。Php应该将字符串和布尔值强制转换为正确的Variant类型,而空的Variation类型应该填充System.Reflection.Missing.Value 的位置

我曾经https://msdn.microsoft.com/en-us/library/office/ff835182(v=office.14).aspx(Word2010)来编译所需的参数列表,并阅读上的评论http://php.net/manual/en/book.com.php找到一个可行的解决方案。。。

到目前为止,唯一可行的解决方案是制作一份副本,打开它,阅读它,删除副本。对我来说,这是最不可取的选择,因为这应该是工作。有很多C++、vb、.net等例子说明了这是如何工作的,但php只是拒绝接受这些参数。我做错了什么?

经过更多的挖掘,在测试了许多失败的东西后,我终于找到了一个以只读方式打开的解决方案,我找到了两种方法,所以我将它们都发布了。

通过剔除剩余价值,它突然奏效了。在这种情况下,我仍然有一个问题要用什么作为"null",这样我就可以跳过那个变量了。但这是另一次的担忧。我还不需要。

通过DOTNET对象

$assembly = 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c';
$class = 'Microsoft.Office.Interop.Word.ApplicationClass';
$w = new DOTNET($assembly, $class);
$w->visible = true;
$DocumentPath = "''''remote''test''alamo.doc";
$d = $w->Documents->Open($DocumentPath,false,true);
echo "Document opened.<br><hr><PRE>";
com_print_typeinfo($d);
$w->Quit();
$w=null;

通过COM对象

$DocumentPath = "''''remote''test''alamo.doc";
$word = new COM("word.application") or die("Unable to instantiate application object");
$wordDocument = new COM("word.document") or die("Unable to instantiate document object");
$MISSING =  1;
$word->Visible = true;
$HTMLPath = "";
try {
echo "<PRE>";
com_print_typeinfo($word->Documents);
echo "</PRE>";
$wordDocument = $word->Documents->Open($DocumentPath/* FileName */,
                                     false/* ConfirmConversions */, 
                                        true/* ReadOnly */);
$HTMLPath = substr_replace($DocumentPath, 'html', -3, 3);
if($wordDocument !== null) {
    $wordDocument->SaveAs($HTMLPath, 3);
}
}
catch(Exception $ex){
    echo $ex->getMessage() . $ex->getTraceAsString();
}
$wordDocument = null;
$word->Quit();
$word = null;

只有一个问题为什么要使用com,它不建议在服务器上使用,特别是在php的上下文中,你应该尝试阅读word 2007/2010.docx文件,这些文件只是xml文件,避免所有的麻烦一起