从Word获取特定页面

时间:2014-02-07 09:27:36

标签: c# office-interop

如何从c#.net控制台应用程序中的单词获取特定页面?

我试过了,

但遗憾的是我的申请中有错误。

以下是我的代码:

{
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();

;
}

所以请给我任何解决方案.. 在此先感谢..

4 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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();

答案 2 :(得分:0)

如果可以,我会使用Open XML SDK 2.5 for Microsoft Office

这使您可以完全访问该文档,我认为最有可能工作,没有任何内存问题。

答案 3 :(得分:0)

我知道它已经很旧了,但是我没有得到正确的答案,所以我的解决方案在这里很好用。

object missing = System.Reflection.Missing.Value; 
var document = application.ActiveDocument;
Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages; 
int num =  aDoc.ComputeStatistics(stat, ref missing);    // Get number of pages

for(int i=0; i<num; i++)
{
    document.ActiveWindow.Selection           // Go to page "i"
            .GoTo(WdGoToItem.wdGoToPage, missing, missing, i.ToString());
    document.ActiveWindow.Selection           // Select whole page
            .GoTo(WdGoToItem.wdGoToBookmark, missing, missing, "\\page");
    document.ActiveWindow.Selection.Copy();   // Copy to clipboard

    // Do whatever you want with the selection
}