Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete any page from a word document. Is there any way to do it?
I can only delete some lines from the page. Is there any way to count the no of lines from the page? Then I can delete the whole page.. Or if there any other way of deleting the page, please help me then. Below I am giving the code that I have done.. which can only delete some lines from a page.
In textbox1, I am taking the value of the page no, which I want to delete.
C#
private void button1_Click(object sender, EventArgs e)
        {
            string filePath = null;
            //First we need to create a new OpenFileDialog:
            OpenFileDialog fDialog = new OpenFileDialog();

            //The title of the OpenFileDialog window:
            fDialog.Title = "Open text/word File";

            //Here we allow to be opened only files with the .txt and .doc extension:
            fDialog.Filter = "Text Files|*.txt|Doc Files|*.docx";

            //we set the InitialDirectory property
            fDialog.InitialDirectory = @"C:\";

            if (fDialog.ShowDialog() == DialogResult.OK)
            {

                //MessageBox.Show(fDialog.FileName.ToString());
                filePath = fDialog.FileName.ToString();
            }

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

              
            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 = 1;
                    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
Comments
Maciej Los 16-Jul-14 16:12pm    
Do not repost!

We have already explained to you in your previous questions that there is no such thing as a page in a word document. A page is an arbitrary collection of lines, that only exists when the user defines the shape and size of the dispaly device that they wish to view the document on. Within the actual document, "page" has no meaning.
 
Share this answer
 
As i mentioned in the comment to the question, please do not repost!

Here is your previous question and answers: Delete a particular page from a word document using C#[^]
 
Share this answer
 

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