Introduction
This article will only show you how to use the COM object library in C#, so you can easily convert a DOC file into different formats that are supported by Microsoft Word. This could be accomplished by using the Microsoft Word 10.0 Object library. You can add this library by using the menu bar of .NET IDE, that is Project � Add Reference�- COM, here you can add the reference to the Microsoft Word 10.0 Object library for your code & it is necessary for the conversion. By adding this library as a reference in your project, you can now use the Word
namespace which will allow you to do all the stuff. So it is necessary to add this reference into your project.
Conversion code
The following code shows how to convert DOC to RTF. You can convert to any format which is supported by MS Word by only changing the type
of Target
file.
using System;
namespace DocConvert
{
class DoctoRtf
{
static void Main()
{
Word.Application newApp = new Word.Application();
object Source="c:\\abc\\Source.doc";
object Target="c:\\abc\\Target.rtf";
object Unknown =Type.Missing;
newApp.Documents.Open(ref Source,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,ref Unknown);
object format = Word.WdSaveFormat.wdFormatRTF;
newApp.ActiveDocument.SaveAs(ref Target,ref format,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown,ref Unknown,
ref Unknown,ref Unknown);
newApp.Quit(ref Unknown,ref Unknown,ref Unknown);
}
}
}
What the code does
If you want to convert the particular file, then you have to first open that file. You can do this by using the Documents
class in the Word
namespace & then call the Open
method which has a number of parameters, out of which source filename is important. Others you can say as missing. Then use the ActiveDocument
�s SaveAs
to save the file in a target format and lastly close all the documents by using Quit
.
Reminders
Make sure that you include the reference to Microsoft Word 10.0 object library in your code; otherwise the compiler will flag error for the Word
namespace.