COM('word.document') 以只读方式打开,而文件由其他文件打开
COM('word.document') open in read only whilst file is opened by other
我正在使用 php 并尝试 fiddle 使用 COM 对象来读取 Word 文件。我真的找不到文档。
我想做的是以只读方式打开一个文件,这样我就不会在主机上看到 "File is in use" 弹出窗口。
如何通过 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 应将字符串和布尔值转换为正确的变体类型,空变体类型应填充 System.Reflection.Missing.Value
的位置
我用 https://msdn.microsoft.com/en-us/library/office/ff835182(v=office.14).aspx (Word 2010) to compile the list of arguments needed, and reading through the comments on http://php.net/manual/en/book.com.php 找到了可行的解决方案...
目前看来唯一可行的解决方案是制作副本,打开副本,阅读,然后删除副本。对我来说,这是最不理想的选择,因为你应该知道,这很有效。大量的 C++、vb、.net 等...这是如何工作的示例,但 php 只是拒绝接受参数。我做错了什么?
只是一个问题,为什么要使用 com,不建议在服务器上使用,特别是在 php 的上下文中,您应该尝试阅读 word 2007/2010 .docx 文件,这些文件只不过是 xml 文件并避免所有麻烦
进一步挖掘,测试了很多失败的东西,我终于找到了一个以只读方式打开的解决方案,而且我找到了两种通往罗马的方式,所以我将它们都发布了。
通过忽略剩余价值,它突然奏效了。仍然留给我一个问题,在这种情况下将什么用作 "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;
我正在使用 php 并尝试 fiddle 使用 COM 对象来读取 Word 文件。我真的找不到文档。
我想做的是以只读方式打开一个文件,这样我就不会在主机上看到 "File is in use" 弹出窗口。
如何通过 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 应将字符串和布尔值转换为正确的变体类型,空变体类型应填充 System.Reflection.Missing.Value
我用 https://msdn.microsoft.com/en-us/library/office/ff835182(v=office.14).aspx (Word 2010) to compile the list of arguments needed, and reading through the comments on http://php.net/manual/en/book.com.php 找到了可行的解决方案...
目前看来唯一可行的解决方案是制作副本,打开副本,阅读,然后删除副本。对我来说,这是最不理想的选择,因为你应该知道,这很有效。大量的 C++、vb、.net 等...这是如何工作的示例,但 php 只是拒绝接受参数。我做错了什么?
只是一个问题,为什么要使用 com,不建议在服务器上使用,特别是在 php 的上下文中,您应该尝试阅读 word 2007/2010 .docx 文件,这些文件只不过是 xml 文件并避免所有麻烦
进一步挖掘,测试了很多失败的东西,我终于找到了一个以只读方式打开的解决方案,而且我找到了两种通往罗马的方式,所以我将它们都发布了。
通过忽略剩余价值,它突然奏效了。仍然留给我一个问题,在这种情况下将什么用作 "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;