c# - 从 Word 获取特定页面

标签 c# office-interop

如何在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();

;
}

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

最佳答案

您使用的是 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 opened.

关于c# - 从 Word 获取特定页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623865/

相关文章:

c# - 为什么 Window.Close 事件会传播?

c# - 通用方法,取消装箱可为 null 的枚举

c# - 检测用户何时打开 word 文档以及何时输入(在 c# 中)

c# - 是否需要使用 Marshal.ReleaseComObject 释放*每个* Excel 互操作对象?

c# - .xps 文件的正确内容类型是什么

c# - PropertyChangedEvent 多次触发

c# - 具有依赖属性的 ContentPresenter 和 Datatemplates

c# - 更多 C# 自动化到 Excel

c# - 如何在Excel工作表的特定位置添加文本框?

c# - 通过反射调用 this[int index]