none
How to delete the last line of word file using C#, Interop.Word ? RRS feed

  • Question

  • Hi everyone,

    Do you know:

    How can I use Interop.Word in C# to delete the last line of file (or page) ?

    Thanks and regards,

    Tai

    Thursday, August 7, 2014 9:51 AM

Answers

  • Hi,

    According to your description, you want to delete the last line of the document with C#.

    In fact, there is no property or method to get the last line of the document directly.

    For a workaround, we can resort to Selection.MoveDown method, which moves the selection down and returns the number of units it's been moved. We can get the count of lines and then use it as the argument "Count" of the Selection.MoveDown method. In this way, wherever the cursor is in the document, we can access to the last line of the document. At last, Selection.Delete Method could be used to delete the last line.

    Here is a C# sample for your reference.

    private void DeleteLastLine()
    {
        Word.Application app = Globals.ThisAddIn.Application;
        Word.Document doc = app.ActiveDocument;
                
        //Get the count of lines
        Microsoft.Office.Core.DocumentProperties properties = doc.BuiltInDocumentProperties;
    
        Microsoft.Office.Core.DocumentProperty LinesCount;
        LinesCount = properties["Number Of Lines"]; 
    
        //Move to the last line
        app.Selection.MoveDown(WdUnits.wdLine, LinesCount);
        app.Selection.Expand(WdUnits.wdLine);
    
        //Delete
        app.Selection.Delete();
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 8, 2014 5:29 AM
    Moderator

All replies

  • Hello Tai,

    See Delete row in Word in VBA .

    Be aware, the Word object model provides the Macro recorder which allows to generate the VBA code in the background for you. Thus, you may know what properties and methods should be called to get the job done. See Create or run a macro for more information.

    Thursday, August 7, 2014 11:09 AM
  • Hi,

    According to your description, you want to delete the last line of the document with C#.

    In fact, there is no property or method to get the last line of the document directly.

    For a workaround, we can resort to Selection.MoveDown method, which moves the selection down and returns the number of units it's been moved. We can get the count of lines and then use it as the argument "Count" of the Selection.MoveDown method. In this way, wherever the cursor is in the document, we can access to the last line of the document. At last, Selection.Delete Method could be used to delete the last line.

    Here is a C# sample for your reference.

    private void DeleteLastLine()
    {
        Word.Application app = Globals.ThisAddIn.Application;
        Word.Document doc = app.ActiveDocument;
                
        //Get the count of lines
        Microsoft.Office.Core.DocumentProperties properties = doc.BuiltInDocumentProperties;
    
        Microsoft.Office.Core.DocumentProperty LinesCount;
        LinesCount = properties["Number Of Lines"]; 
    
        //Move to the last line
        app.Selection.MoveDown(WdUnits.wdLine, LinesCount);
        app.Selection.Expand(WdUnits.wdLine);
    
        //Delete
        app.Selection.Delete();
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 8, 2014 5:29 AM
    Moderator