使用 C# 在 docx 中选择页面 [英] Select page in docx using C#

查看:23
本文介绍了使用 C# 在 docx 中选择页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个变量中获取整个 word 文档,如下所示:

I'm currently getting the entire word document in a variable as below:

docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();

Wholestory() 函数选择整个 word 文档.

The wholestory() function selects the entire word document.

如果我可以逐页选择,请建议我.

Please suggest me if i can select page by page.

推荐答案

您需要设置要选择的范围.

You need to set range, which you want to select.

示例:

object what = WdGoToItem.wdGoToPage;
object which = WdGoToDirection.wdGoToAbsolute;
object count = 0;

const string fileName = "C:\1.docx";
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);

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

 //if you want to select last page
 if (endRange.Start == startRange.Start)
 {
   which = WdGoToDirection.wdGoToLast;
   what = WdGoToItem.wdGoToLine;
   endRange = wordApplication.Selection.GoTo(ref what, ref which, ref count2, ref missing);
 }

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

这篇关于使用 C# 在 docx 中选择页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆