Question

I have RTF data in a string str and i would like that data to be loaded in MS word object. I've seen Documents.Open()method but it needs a physical file path to read a particular file and i don't have such file.

How do i open new instance of Word and load my RTF data in it ?

Microsoft.Office.Interop.Word.ApplicationClass wordapp = new ApplicationClass();
wordapp.Visible = false;

string str = @"{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl{\f0\fnil\fcharset0\fprq2 Arial;}{\f1\fswiss\fcharset0\fprq2 Arial;}{\f2\froman\fcharset2\fprq2 Symbol;}}
{\colortbl;}{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}{\*\generator TX_RTF32 17.0.540.502;}
\paperw12240\paperh15840\margl1138\margt1138\margr1138\margb1138\deftab1134\widowctrl\formshade\sectd\headery720\footery720\pgwsxn12240\pghsxn15840\marglsxn1138\margtsxn1138\margrsxn1138\margbsxn1138\pgbrdropt32\pard\itap0\nowidctlpar\plain\f1\fs20 test1\par }";

I will be doing some formatting in word but the applicationClass should be wordapp.Visible = false;

UPDATE: I wouldn't prefer the file to be saved on system. isn't there anyway to read and work without saving it on physical memory ?

No correct solution

OTHER TIPS

Instead of trying to load the RTF text into Word, I think it's better to load it into a text file (since RTF files are plain text anyway), then open that raw RTF text file as a new Word document. This causes Word to process the RTF text and format it as a Word doc (with the fonts/margins/etc), which I think is what you're looking for, not to see the RTF text verbatim. Once it's a new Word doc, you can do your manipulations and then save to a Word doc file:

        string filePath = @"c:\rawRtfText.rtf";

        //write the raw RTF string to a text file.
        System.IO.StreamWriter rawTextFile = new System.IO.StreamWriter(filePath, false);
        string str = @"{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl{\f0\fnil\fcharset0\fprq2 Arial;}{\f1\fswiss\fcharset0\fprq2 Arial;}{\f2\froman\fcharset2\fprq2 Symbol;}}{\colortbl;}{\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}{\*\generator TX_RTF32 17.0.540.502;}\paperw12240\paperh15840\margl1138\margt1138\margr1138\margb1138\deftab1134\widowctrl\formshade\sectd\headery720\footery720\pgwsxn12240\pghsxn15840\marglsxn1138\margtsxn1138\margrsxn1138\margbsxn1138\pgbrdropt32\pard\itap0\nowidctlpar\plain\f1\fs20 test1\par }";
        rawTextFile.Write(str);
        rawTextFile.Close();

        //now open the RTF file using word.
        Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();
        msWord.Visible = false;
        Microsoft.Office.Interop.Word.Document wordDoc = msWord.Documents.Open(filePath);

        //after manipulating the word doc, save it as a word doc.
        object oMissing = System.Reflection.Missing.Value;
        wordDoc.SaveAs(@"c:\RtfConvertedToWord.doc", ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top