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 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 to add a reference, we can select the Microsoft COM library to 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.
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 open 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 placeholder to tell Word to use the default value.
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 Microsoft Word
object missing = Type.Missing;
object trueIndicator = true;
app.Documents.Open(
FileName: reftempOutputPath,
ConfirmConversions: missing,
ReadOnly: missing,
AddToRecentFiles: missing,
PasswordDocument: missing,
PasswordTemplate: missing,
Revert: missing,
WritePasswordDocument: missing,
WritePasswordTemplate: missing,
Format: missing,
Encoding: missing,
Visible: ref trueIndicator,
OpenAndRepair: missing,
DocumentDirection: missing,
NoEncodingDialog: missing,
XMLTransform: missing
);
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
string tempOutputPath = @"c:\temp\tmprichtextbox.rtf";
richTextBox1.SaveFile(tempOutputPath);
// Open the RTF file inside Microsoft Word
object missing = Type.Missing;
object trueIndicator = true;
app.Documents.Open(
FileName: tempOutputPath,
ConfirmConversions: missing,
ReadOnly: missing,
AddToRecentFiles: missing,
PasswordDocument: missing,
PasswordTemplate: missing,
Revert: missing,
WritePasswordDocument: missing,
WritePasswordTemplate: missing,
Format: missing,
Encoding: missing,
Visible: ref trueIndicator,
OpenAndRepair: missing,
DocumentDirection: missing,
NoEncodingDialog: missing,
XMLTransform: missing
);
}
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 developer's 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(reftempOutputPath, refformat, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, reftypeMissing, ref trueIndicator, reftypeMissing, reftypeMissing, reftypeMissing);
richTextBox1.LoadFile((string)tempOutputPath);
You can also use this code to help you translate between rtf and doc format in Word. Anyway, enjoy the COMing effect of C# and .NET as you wrestle with utilizing the built-in programming interfaces inside of Microsoft Office.