none
Reading and Writting RTF into and from word document dynamically

    Question

  • Hi everyone

     

    I just started working with VSTO today and one of the thing I really needs to do is to be able to insert RTF into the word document, I came up with a nasty solution which does the job:

     

    For insert:

     

               try

                {

                    object start = 0, end = 0;

                    Word.Range r = this.Range(ref start, ref end);

                    Clipboard.SetText(@"{\rtf1\adeflang1025\ansi\ansicpg1252\uc1\adeff0\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f1\fswiss\fcharset0\fprq2{\*\panose 020b0604020202020204}Arial;}{\f37\froman\fcharset238\fprq2 Times New Roman CE;} " +

        @"{\f38\froman\fcharset204\fprq2 Times New Roman Cyr;}{\f40\froman\fcharset161\fprq2 Times New Roman Greek;}{\f41\froman\fcharset162\fprq2 Times New Roman Tur;}{\f42\fbidi \froman\fcharset177\fprq2 Times New Roman (Hebrew);} " +

        @"{\f43\fbidi \froman\fcharset178\fprq2 Times New Roman (Arabic);}{\f44\froman\fcharset186\fprq2 Times New Roman Baltic;}{\f45\froman\fcharset163\fprq2 Times New Roman (Vietnamese);}{\f47\fswiss\fcharset238\fprq2 Arial CE;} " +

        @"{\f48\fswiss\fcharset204\fprq2 Arial Cyr;}{\f50\fswiss\fcharset161\fprq2 Arial Greek;}{\f51\fswiss\fcharset162\fprq2 Arial Tur;}{\f52\fbidi \fswiss\fcharset177\fprq2 Arial (Hebrew);}{\f53\fbidi \fswiss\fcharset178\fprq2 Arial (Arabic);} " +

        @"{\f54\fswiss\fcharset186\fprq2 Arial Baltic;}{\f55\fswiss\fcharset163\fprq2 Arial (Vietnamese);}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0; " +

        @"\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;}{\stylesheet{ " +

        @"\ql \li0\ri0\widctlpar\wrapdefault\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \rtlch\fcs1 \af0\afs24\alang1025 \ltrch\fcs0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \snext0 Normal;}{\*\cs10 \additive \ssemihidden  " +

        @"Default Paragraph Font;}{\*\ts11\tsrowd\trftsWidthB3\trpaddl108\trpaddr108\trpaddfl3\trpaddft3\trpaddfb3\trpaddfr3\trcbpat1\trcfpat1\tblind0\tblindtype3\tscellwidthfts0\tsvertalt\tsbrdrt\tsbrdrl\tsbrdrb\tsbrdrr\tsbrdrdgl\tsbrdrdgr\tsbrdrh\tsbrdrv  " +

        @"\ql \li0\ri0\widctlpar\wrapdefault\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \rtlch\fcs1 \af0\afs20 \ltrch\fcs0 \fs20\lang1024\langfe1024\cgrid\langnp1024\langfenp1024 \snext11 \ssemihidden Normal Table;}} " +

        @"{\*\latentstyles\lsdstimax156\lsdlockeddef0}{\*\rsidtbl \rsid10297789\rsid15884700}{\*\generator Microsoft Word 11.0.0000;}{\info{\operator Matt Majedi}{\creatim\yr2007\mo11\dy22\hr14\min5}{\revtim\yr2007\mo11\dy22\hr14\min5}{\version2}{\edmins0} " +

        @"{\nofpages1}{\nofwords3}{\nofchars19}{\nofcharsws21}{\vern24611}{\*\password 00000000}}{\*\xmlnstbl {\xmlns1 http://schemas.microsoft.com/office/word/2003/wordml}}\paperw12240\paperh15840\margl1800\margr1800\margt1440\margb1440\gutter0\ltrsect " +

        @"\widowctrl\ftnbj\aenddoc\donotembedsysfont0\donotembedlingdata1\grfdocevents0\validatexml0\showplaceholdtext0\ignoremixedcontent0\saveinvalidxml0\showxmlerrors0\horzdoc\dghspace120\dgvspace120\dghorigin1701\dgvorigin1984\dghshow0\dgvshow3 " +

        @"\jcompress\viewkind4\viewscale100\rsidroot15884700 \fet0{\*\wgrffmtfilter 013f}\ilfomacatclnup0\ltrpar \sectd \ltrsect\linex0\sectdefaultcl\sftnbj {\*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang {\pntxta .}}{\*\pnseclvl2 " +

        @"\pnucltr\pnstart1\pnindent720\pnhang {\pntxta .}}{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang {\pntxta .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang {\pntxta )}}{\*\pnseclvl5\pndec\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl6 " +

        @"\pnlcltr\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl8\pnlcltr\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang " +

        @"{\pntxtb (}{\pntxta )}}\pard\plain \ltrpar\ql \li0\ri0\nowidctlpar\wrapdefault\faauto\rin0\lin0\itap0 \rtlch\fcs1 \af0\afs24\alang1025 \ltrch\fcs0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {\rtlch\fcs1 \ab\af1\afs20 \ltrch\fcs0 " +

        @"\b\f1\fs20\insrsid10297789 Hello My name is Matt}{\rtlch\fcs1 \ab\af1\afs20 \ltrch\fcs0 \b\f1\fs20\insrsid10297789\charrsid10297789 " +

        @"\par }}", TextDataFormat.Rtf);

                    r.Paste();

                }

                catch { }

     

    in order to read the document as RTF:

     

               object start = Globals.ThisDocument.Content.Start;

                object end = Globals.ThisDocument.Content.End;

                Word.Range r = Globals.ThisDocument.Range(ref start, ref end);

                r.Copy();

     

                textBox1.Text = Clipboard.GetText(TextDataFormat.Rtf);

     

    basically I just use clipboard and surprisingly its working.

    Is there a proper way to do this?

     

    Many Thanks

     

    Thursday, November 22, 2007 4:43 PM

Answers

  • Word has never supported RTF as a "native" format. A converter has always been required (and supplied with the application). In order for the converter to trigger, the RTF either needs to be pasted in from the clipboard or loaded from a file.

     

    The alternative to your approach would be to stream the RTF to a (text) file (with the extension *.rtf) then use the Open or the InsertFile method to bring it into Word.

     

    Friday, November 23, 2007 1:41 PM

All replies

  • Word has never supported RTF as a "native" format. A converter has always been required (and supplied with the application). In order for the converter to trigger, the RTF either needs to be pasted in from the clipboard or loaded from a file.

     

    The alternative to your approach would be to stream the RTF to a (text) file (with the extension *.rtf) then use the Open or the InsertFile method to bring it into Word.

     

    Friday, November 23, 2007 1:41 PM
  • Thanks Cindy

     

    Monday, November 26, 2007 9:43 AM