使用C#在docx中选择页面

我目前在一个变量中得到整个word文档,如下所示:

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

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

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

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

例:

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