[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Convert RTF and TXT files into DOCX files in C#

[Convert RTF and TXT files into DOCX files in C#] First add a reference to Microsoft.Office.Interop.Word. To make using the library easier, add this using statement:

using Word = Microsoft.Office.Interop.Word;

When you enter the input and output file names and click Convert, the program uses the following code to open the file and save it in the new format.

// Get the Word application object. Word._Application word_app = new Word.ApplicationClass(); // Make Word visible (optional). //word_app.Visible = true; // Open the file. object input_file = txtInputFile.Text; object missing = Type.Missing; word_app.Documents.Open(ref input_file, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); // Save the output file. object output_file = txtOutputFile.Text; object format_doc = (int)16; // 16 for docx, 0 for doc. Word._Document active_document = word_app.ActiveDocument; active_document.SaveAs(ref output_file, ref format_doc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); // Exit the server without prompting. object false_obj = false; active_document.Close(ref false_obj, ref missing, ref missing); word_app.Quit(ref missing, ref missing, ref missing); MessageBox.Show("Done");

The code first creates a Word application object. It uses that object's Documents.Open method to open the input file. It then creates a Word._Document object to represent the active document. If you don't do that, there's an ambiguity about how the SaveAs method should be called so you get an error.

The code calls the document's SaveAs method passing it a reference to the variable format_doc to tell it to save in Word .docx format. Notice how all parameters are objects passed by reference because that's what Word needs. Notice also how the code uses the special value missing to indicate a missing parameter and make the Word method use a default value.

The code next calls the document's Close method, passing it the value false (again as a reference to an object) so it doesn't save changes. It finishes by calling the Word server's Quit method to close Word the server.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.