2014-02-07 105 views
1

如何在c#.net控制檯應用程序中從單詞獲取特定頁面?從Word獲取特定頁面

我已經嘗試過了,

但不幸的是我有從我的應用程序錯誤。

以下是我的代碼:

{ 
object what = WdGoToItem.wdGoToPage; 

object which = WdGoToDirection.wdGoToFirst; 

object count = 0; 

const string fileName = @"C:\..\..\test.doc"; 

object fileNameAsObject = fileName; 

Application wordApplication = new Application(); 

object readOnly = false; 

object missing = System.Reflection.Missing.Value; 

wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing, 
ref missing, ref missing, ref missing, ref missing, 
ref missing, ref missing, ref missing, ref missing, 
ref missing, ref missing, ref missing, ref missing); 

// here on this following line I have got error "This method or property is not available because this command is not available for reading." 
Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing); 


object count2 = (int)count + 1; 

Range endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing); 



endRange.SetRange(startRange.Start, endRange.End); 
endRange.Select(); 

; 
} 

所以,請給我提供任何解決方案就可以了.. 在此先感謝..

+0

請給儘可能多的細節儘可能爲您使用此技術有什麼錯誤 – Coops

+0

也?有[微軟的Office Open XML SDK 2.5](http://www.microsoft.com/en-gb/download/details.aspx?id=30425)的喜歡 – Coops

回答

0

後首先調用Word應用程序還是塊文件。因此文檔是隻讀的。 殺WINWORD.EXE進程,然後更改代碼:

Document document = wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing, 
                  ref missing, ref missing, ref missing, ref missing, 
                  ref missing, ref missing, ref missing, ref missing, 
                  ref missing, ref missing, ref missing, ref missing); 

工作有密切的文檔後:

document.Close(); 
wordApplication.Quit(); 

之後修改工作代碼:

 object what = WdGoToItem.wdGoToPage; 

     object which = WdGoToDirection.wdGoToFirst; 

     object count = 0; 

     const string fileName = @"C:\..\..\test.doc"; 

     object fileNameAsObject = fileName; 

     Application wordApplication = new Application(); 

     object readOnly = false; 

     object missing = System.Reflection.Missing.Value; 

     wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing, 
                  ref missing, ref missing, ref missing, ref missing, 
                  ref missing, ref missing, ref missing, ref missing, 
                  ref missing, ref missing, ref missing, ref missing); 

     // here on this following line I have got error "This method or property is not available because this command is not available for reading." 
     Range startRange = wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing); 


     object count2 = (int)count + 1; 

     Range endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing); 



     endRange.SetRange(startRange.Start, endRange.End); 
     endRange.Select(); 

     wordApplication.Documents.Close(); 
     wordApplication.Quit(); 
+0

不過它提供了同樣的錯誤... –

+0

是你以前殺死了WINWORD.EXE進程? – neodim

+0

它將如何工作?其實我第一次聽到這個殺了WINWORD.EXE,而且什麼時候會運行,怎麼樣? –

5

你使用Office 2013?在針對新安裝的Office 2013運行我們的互操作代碼時,我們遇到了同樣的錯誤消息。這似乎是由於Office 2013的默認「閱讀模式」,如here所述。

試圖通過將Application.ActiveWindow.View.ReadingLayout爲false(如在文章的評論中提到)關閉閱讀模式。您打開文檔後必須執行此調用。否則,調用將失敗與消息:System.Runtime.InteropServices.COMException : This command is not available because no document is open.

+0

爲我工作。謝謝你,先生! –