none
VSTO Word 2007 insert paragraph in the last page

    Question

  • Hi all, i'm new in VSTO, i almost done every requirement using tutorials and search engines, but i cant get info on how to get the last page of a word document. I'm using vsto 2007.

    I'll appreciate any help.

    Thanks in advance from Argentina.

    Monday, August 13, 2012 4:42 PM

Answers

  • Here. I just tested it with VSTO and Word 2007. It assumes you have

    using Word = Microsoft.Office.Interop.Word;
    Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
    Word.Paragraph paragraph;
    int totalPages;
    Word.Range range;
    
    // Get a range containing the whole document
    range = document.Content.Duplicate;
    
    // Get the total number of pages in range
    totalPages = range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
    
    // Set our range to contain only the last page
    range = range.GoTo(Word.WdGoToItem.wdGoToPage, Type.Missing, totalPages, Type.Missing);
    
    // Insert new paragraph
    paragraph = document.Paragraphs.Add(range);
    
    // Test new paragraph
    paragraph.Range.Text = "This is our new paragraph!" + "\r";

    It takes your question literally, assuming you want a paragraph at the beginning of the last page in a document. If, instead, you want a paragraph at the very end of a document, see my edited post above.

    If you're wondering why the final line has + "\r", it's because for some reason, when testing it, I found the Paragraphs.Add did not return a proper paragraph. If you use "\r" Word will insert a paragraph break.

    • Marked as answer by Holden-ar Tuesday, August 14, 2012 1:51 AM
    Monday, August 13, 2012 8:39 PM

All replies

  • Argentina,

    If you say what language you're using, we can give you a code sample.

    Basically you get the document range from document.Content; you find the page count through the Information member, and use the GoTo command to set the range to the last page.

    Edit: But if you want to add a paragraph to the very end of a document, it's easier....in C# it would be simply:

    myDocument.Content.Paragraphs.Add();

    Or...I think this would also work:

    myDocument.Paragraphs.Add();

    • Edited by JosephFox Monday, August 13, 2012 8:02 PM
    Monday, August 13, 2012 7:56 PM
  • I'm using C#, i'll appreciate your code sample. Thanks!
    Monday, August 13, 2012 7:58 PM
  • Here. I just tested it with VSTO and Word 2007. It assumes you have

    using Word = Microsoft.Office.Interop.Word;
    Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
    Word.Paragraph paragraph;
    int totalPages;
    Word.Range range;
    
    // Get a range containing the whole document
    range = document.Content.Duplicate;
    
    // Get the total number of pages in range
    totalPages = range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
    
    // Set our range to contain only the last page
    range = range.GoTo(Word.WdGoToItem.wdGoToPage, Type.Missing, totalPages, Type.Missing);
    
    // Insert new paragraph
    paragraph = document.Paragraphs.Add(range);
    
    // Test new paragraph
    paragraph.Range.Text = "This is our new paragraph!" + "\r";

    It takes your question literally, assuming you want a paragraph at the beginning of the last page in a document. If, instead, you want a paragraph at the very end of a document, see my edited post above.

    If you're wondering why the final line has + "\r", it's because for some reason, when testing it, I found the Paragraphs.Add did not return a proper paragraph. If you use "\r" Word will insert a paragraph break.

    • Marked as answer by Holden-ar Tuesday, August 14, 2012 1:51 AM
    Monday, August 13, 2012 8:39 PM
  • thank you very much for your time and your explanation. 
    Tuesday, August 14, 2012 1:51 AM