Color Syntax Editor Part II - Exporting a RichTextBox to a Microsoft Word Document

ExportRTFToWord.jpg

Figure 1 - Exporting a RichTextBox to a Word Document

Introduction

A while back, I wrote an article describing how to create a color syntax editor to allow you to edit code for PHP and other languages.  I thought a nice feature to add to the editor would be the ability to export the  contents of the editor into a Word Document.  Using the Word COM library, this is a fairly easy task.  You just need to know (1) how to get the contents of a RichTextBox into an rtf file and (2) how to open that file inside of Word Programmatically.

Exporting RTF

It turns out that the RichTextBox in .NET is fairly feature rich as well.  You can serialize the contents of a RichTextBox simply by calling the method SaveFile and specifying the name of the file you wish to save. For this application, we'll simply save the contents to a temporary file that we can later open in Word.

Bringing RTF into Word

Word can open many formats including RTF.  In order to access Microsoft Word in our application, we need to bring the Microsoft Word COM library into our application.  By right clicking on our project and choosing add reference we can select the Microsoft COM library into the project  as shown in Figure 2. Note that we need to go to the COM tab to bring in the Interop Library for Microsoft Word.

WordReference.JPG

Figure 2 - Getting a Reference to the Microsoft Word COM Library

Now we have the tools we need to open our RTF file into Microsoft Word.  First we'll add a using statement at the top of our Form to allow us to easily access the Word namespace:

using Microsoft.Office.Interop.Word;

Next we'll add the code we need to open a Word Application.  Opening a Word Application simply requires us to create the Word application object and make it Visible.

Listing 1 - Opening Microsoft Word from C#

// open Microsoft Word

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible =
true;

Finally, we can use the Documents collection property of the Application to open our RTF file.  The Documents property has an Open method that takes about 20 parameters as shown in figure 3:

Figure 3 - Open Method for Word in C#

Most of these parameters we don't care about .  The bad news is that because the CCW (COM Callable Wrapper) around Microsoft Word has such a strange translation of the call to opening a document, we need to pass all of these parameters.  The good news is we can use the Type.Missing object in almost all the parameters.   Note that we also have to pass everything by reference to an object.  In most situations this makes absolutely no sense, however, to understand why this is the case you have to go back to VBA.  Microsoft designed these COM calls so that all of these parameters were optional.  The rule for optional parameters is that if a parameter is optional, it must be a variant.  When a variant is translated back to a COM Callable Wrapper it becomes a reference to an object.  In the CCW, the parameter is still optional, but you have to pass the Type.Missing object as a place holder to tell Word to use the default value (See this MS Topic on the subject for anything I may have missed here).

So, to make a long story short, our code to read the RTF file is shown in listing 2:

Listing 2 - Opening an RTF file in Microsoft Word from C#

// open the rtf file inside of Microsoft Word

object typeMissing = Type.Missing;

object trueIndicator = true;

app.Documents.Open(ref tempOutputPath, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref trueIndicator, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing);

Putting it all together

Now that we understand how to save a rich text box to an RTF file and how to open the RTF file in word,  we can easily add this functionality to our color syntax editor.  Listing 3 gives you the full picture of how the rtf export to word is done from a menu event handler:

Listing 3 - Exporting the RichTextBox to Word

private void menuItem4_Click(object sender, EventArgs e)
 {
  
// open Microsoft Word
  Microsoft.Office.Interop.Word.
Application app = new Microsoft.Office.Interop.Word.Application();
  app.Visible =
true;

// save the contents of the rtf file to a temporary file
object tempOutputPath = @"c:\temp\tmprichtextbox.rtf";
richTextBox1.SaveFile((
string)tempOutputPath);

// open the rtf file inside of Microsoft Word
object typeMissing = Type.Missing;

object trueIndicator = true;

app.Documents.Open(ref tempOutputPath, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref trueIndicator, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing);
}

Conclusion

As you can see, C# easily allows us to add features to our applications that interact with all of the Microsoft Office Applications.  Microsoft is continually seeking ways to improve the developers interaction with their Office tools using .NET so that developers can unleash all the power that Office has to offer. The example in this article showed us how to export the contents of a RichTextBox to Word.  We could have just as easily imported the contents of word into the RichTextBox:

object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;
app.ActiveDocument.SaveAs(
ref tempOutputPath, ref format, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref typeMissing, ref trueIndicator, ref typeMissing, ref typeMissing, ref typeMissing);

richTextBox1.LoadFile((string)tempOutputPath);

You can also use this code to help you translate between rtf and doc format in Word.  Anyway, enjoy COMing effect of C# and .NET as you wrestle with the utilizing the built-in programming interfaces inside of Microsoft Office.

Up Next
    Ebook Download
    View all
    Learn
    View all