互操作字 - 删除页面从文档页面、操作、文档

2023-09-04 22:38:21 作者:余悸

什么是删除使用Word互操作库文档对象特定页面的最简单和最有效的方法是什么?

What is the easiest and most efficient way to delete a specific page from a Document object using the Word Interop Libraries?

我注意到有一个扩展/实现IEnumerable一个页面属性。可以简单地删除该数组中的元素和页面会从文件中删除?

I have noticed there is a Pages property that extends/implements IEnumerable. Can one simply remove the elements in the array and the pages will be removed from the Document?

我也看到了范围和节中的示例,但不看很优雅的使用。

I have also seen the Ranges and Section examples, but the don't look very elegant to use.

感谢。

推荐答案

一个可能的选择是书签整个页面(选择整个页面,请转到工具|插入书签然后键入一个名称)。然后,您可以使用文档对象的书签收藏参考文本并删除它。

One possible option is to bookmark the whole pages (Select the whole page, go to Tools | Insert Bookmark then type in a name). You can then use the Bookmarks collection of the Document object to refer to the text and delete it.

另外,试试C#中的等价code:

Alternatively, try the C# equivalent of this code:

Doc.ActiveWindow.Selection.GoTo wdPage, PageNumber
Doc.Bookmarks("\Page").Range.Text = ""

的第一行移动光标到网页的PageNumber。第二个使用了predefined书签总是指的是网页目前光标在,包括在页面结束时,如果它存在于分页符。

The first line moves the cursor to page "PageNumber". The second one uses a Predefined Bookmark which always refers to the page the cursor is currently on, including the the page break at the end of the page if it exists.