2

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

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

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

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

4

3 回答 3

4

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

例子:

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();
于 2012-08-06T16:59:50.517 回答
0

你应该能够做这样的事情:

我们如何在 c sharp 中打开具有特定页码的 word 文件?

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count = 3;

wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
于 2012-08-04T07:32:54.310 回答
0

这是在 Google 上弹出的第一个项目。为了节省将来我的时间,如果你想去书签,你可以做这样的事情。

    private void WordGoto(string _name)
    {
        object what = WdGoToItem.wdGoToBookmark;
        object which = WdGoToDirection.wdGoToAbsolute;
        object name = _name;
        //object count = 0;
        object missing = System.Reflection.Missing.Value;
        Wordapp.Selection.GoTo(ref what, ref missing, ref missing, ref name);
    }
于 2021-04-08T12:09:53.300 回答