Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to delete a particular page from a word document.
Suppose I have a word document. i want to delete the 3rd page of that document using C#.
How shall I do it?
I tried this.. but i can only delete some lines of a page. But not the whole page of a document. As for example, here in my code I have deleted only 5 lines..but I was to delete the whole page. In textbox1, I am taking the value of the page no which I want to delete.

C#
object missing = System.Reflection.Missing.Value;
object fileName = filePath;
object readOnly = false;
object isVisible = true;

//Start Word and open a document.
Word._Application oWord;
Word._Document oDoc1, oDoc2;
oWord = new Word.Application();
oWord.Visible = true;

oDoc1 = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref isVisible, ref missing,
    ref missing, ref missing, ref missing);

//Goto some specific page and insert a blank page or page break
int y = 0;
int.TryParse(textBox1.Text.ToString(), out y);
object gotoPage = Word.WdGoToItem.wdGoToPage;
object gotoNext = Word.WdGoToDirection.wdGoToNext;
object gotoCount = null;
object gotoName = y;

object unit = Word.WdUnits.wdLine;

//var lineCount =File.ReadLines(filePath).Count();
//The lines you want to delete...
object count = 5;
object extend = Word.WdMovementType.wdExtend;

oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);
oWord.Selection.MoveDown(ref unit, ref count, ref extend);
oWord.Selection.TypeBackspace();
Posted
Updated 13-Jul-14 0:13am
v2

1 solution

You can't! There is no functionality like:
VB
ActiveDocument.Pages(NoOfPage).Delete
'or
ActiveDocument.Pages(NoOfPage).Remove

Page is used to visualize document layout. You need to select all paragraphs from that page, then you'll be able to delete "page".

More:
Page Object (Word)[^]
Solved: Delete a Page in Word 2003 via VBA?[^]
Selecting or referring to a page in the Word object model[^]

[EDIT]
I wrote macro which selects all paragraphs on referenced page and delete them.
VB
Sub Test()

DeletePage ActiveDocument, 2

End Sub


Sub DeletePage(ByVal doc As Document, ByVal iPage As Integer)
Dim rngStart As Range, rngEnd As Range, i As Integer

Set rngStart = doc.GoTo(What:=wdGoToPage, Which:=iPage)
rngStart.Select
Set rngEnd = rngStart
Do While i < iPage + 1
    Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
    Set rngEnd = Selection.Range
    i = rngEnd.Information(wdActiveEndPageNumber)
    If i > iPage Then
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        Set rngEnd = Selection.Range
        Exit Do
    End If
Loop

doc.Range(Start:=rngStart.Start, End:=rngEnd.End).Select

If MsgBox("Are you sure you want to delete " & iPage & " page?", vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
    Selection.Delete
End If

End Sub


Note: It's ineffective way.

[/EDIT]
 
Share this answer
 
v2
Comments
Valery Possoz 17-Jul-14 17:35pm    
Hello,

Actually I think it may be possible using OpenXML. When the document is rendered, each rendered page break in the document object model is marked with a LastRenderedPageBreak. see here: http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.lastrenderedpagebreak(v=office.14).aspx

You need to delete all the elements between 2 LastRenderedPageBreak. This is where it becomes tricky as LastRenderedPageBreak can be anywhere in the document elements tree and you'll need to re-create a coherent tree.

I think it might be quite a complex task!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900