如何在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();
;
}所以请给我任何解决方案..提前谢谢..
发布于 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.
发布于 2014-02-07 17:42:40
在第一次调用后,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();发布于 2014-02-07 17:44:01
如果可以,我会使用Open XML SDK 2.5 for Microsoft Office
这给了你对文档的完全访问权限,在我看来,这是最有可能工作的,而且没有任何内存问题。
https://stackoverflow.com/questions/21623865
复制相似问题