2015-04-01 123 views
0

我使用php並嘗試使用COM對象來讀取Word文件。我發現文檔無處可尋。COM('word.document')打開只讀,而其他文件被打開

我想要做的是打開一個只讀的文件,所以我沒有得到主機上的「文件正在使用」彈出。

如何通過COM告訴單詞將文件作爲只讀文件打開?我想使用的變體,但我得到了以下錯誤:

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中)的地方,編譯需要的參數列表中,並通過註釋上http://php.net/manual/en/book.com.php閱讀找到一個可行的解決方案...

到目前爲止似乎工作的唯一解決方案是製作副本,打開它,讀取它,刪除副本。對我而言,這是最不可取的選擇,因爲這應該你知道,工作。大量的C++,VB,NET等...這是如何工作的例子,但PHP只是拒絕接受參數。我究竟做錯了什麼?

回答

0

挖掘一些,測試了許多失敗的事情我終於找到了一個解決方案,在只讀中打開,我發現了兩種羅馬方式,所以我將它們發佈。

通過忽略剩餘值,它突然發揮作用。在這種情況下,我仍然把這個問題留在什麼地方作爲「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; 
0

只是一個問題,爲什麼使用com,這是不建議在服務器上使用,特別是在PHP的情況下,你應該嘗試閱讀word 2007/2010 .docx文件,這些文件只是xml文件,並避免所有hassels全部在一起

+0

我讀的文件是doc格式。我願意將它們轉換爲docx,我可以在其上放置一些廉價勞動力,但是你能否指出我讀取了docx的api?到目前爲止,我發現的所有東西都是COM – Tschallacka 2015-04-01 11:47:15

+0

而且,我更喜歡使用Com對象,因爲它使得轉換爲PDF變得容易很多。 – Tschallacka 2015-04-01 11:59:50

+0

我可以給你指出讀取xlsx文件的代碼,這應該會給你一個開始 – Adik 2015-04-02 03:18:15