Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interop Word - Delete Page from Document

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

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.

Thanks.

like image 899
Koekiebox Avatar asked Apr 06 '26 05:04

Koekiebox


1 Answers

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.

Alternatively, try the C# equivalent of this code:

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

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.

like image 183
Foole Avatar answered Apr 08 '26 19:04

Foole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!