BeeGees
BeeGees

Reputation: 481

In c# using microsoft.office.tools.word how do we save a single page as rtf file

In c# using microsoft.office.tools.word how do we save a single page of a document as rtf file.

Lets say only Page 5 has to be saved as a rtf.

I cannot see any thing like Document.Pages[5] to access a certain page.

Upvotes: 0

Views: 461

Answers (1)

Luuk
Luuk

Reputation: 14929

This saves page 2 as "page2.pdf"

    Document d = wordApp.ActiveDocument;

    object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
    object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
    object count = 2;

    Range r =wordApp.Selection.GoTo(ref what, ref which, ref count);
    count = (int)count+1;
    Range r2 = wordApp.Selection.GoTo(ref what, ref which, ref count);
    r.End = r2.End;
    r.Select();

    r.ExportAsFixedFormat(@"d:\temp\page2.pdf", WdExportFormat.wdExportFormatPDF);

It should give ideas on how to select another page, and saving it as RTF...

Pages are another story:

Pages pages = wordApp.ActiveDocument.ActiveWindow.ActivePane.Pages;
MessageBox.Show("This document has "+pages.Count+" pages");

But my knowledge about this is limited enough to stop after these 2 lines ... 😁

EDIT: I could not find a solution to write the selected text to a RTF but, you can always copy it to a new document, and then SaveAs....

With the selection:

        r.Copy();
        Document dtmp = wordApp.Documents.Add();
        dtmp.Activate();
        Selection sel = wordApp.ActiveWindow.ActivePane.Selection;
        sel.Paste();
        dtmp.SaveAs2(@"d:\temp\page2.rtf", WdSaveFormat.wdFormatRTF);
        dtmp.Close();

Upvotes: 1

Related Questions