首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Word获取特定页面

从Word获取特定页面
EN

Stack Overflow用户
提问于 2014-02-07 17:27:36
回答 4查看 2.1K关注 0票数 1

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

我试过了,

但不幸的是,我的应用程序出现了错误。

以下是我的代码:

代码语言:javascript
运行
复制
{
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();

;
}

所以请给我任何解决方案..提前谢谢..

EN

回答 4

Stack Overflow用户

发布于 2015-03-26 10:49:03

您是否在使用Office 2013?当我们在新安装的Office 2013上运行互操作代码时,我们遇到了同样的错误消息。这似乎是由于Office2013的默认“阅读模式”,正如here提到的。

尝试通过将Application.ActiveWindow.View.ReadingLayout设置为false来关闭阅读模式(如本文的评论中所述)。此调用必须在打开文档后执行。否则,调用将失败,并显示消息:System.Runtime.InteropServices.COMException : This command is not available because no document is open.

票数 5
EN

Stack Overflow用户

发布于 2014-02-07 17:42:40

在第一次调用后,Word应用程序仍然会阻止文档。因此,文档是只读的。终止WINWORD.EXE进程,然后将代码更改为:

代码语言:javascript
运行
复制
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);

下班后关闭文档:

代码语言:javascript
运行
复制
document.Close();
wordApplication.Quit();

修改工作代码后:

代码语言:javascript
运行
复制
        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
EN

Stack Overflow用户

发布于 2014-02-07 17:44:01

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

这给了你对文档的完全访问权限,在我看来,这是最有可能工作的,而且没有任何内存问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21623865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档