先在項目引用裡添加上對Microsoft Word 9.0 object library的引用。
using System;
namespace DocConvert
{
class DoctoRtf
{
static void Main()
{
//創建一個Word的實例
Word.Application newApp = new Word.Application();
// 指定源文件和目標文件
object Source="c:\\abc\\Source.doc";
object Target="c:\\abc\\Target.rtf";
object Unknown =Type.Missing;
// 打開要轉換的Word文件
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 );
// 指定文檔的類型
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);
//關閉Word實例
newApp.Quit(ref Unknown,ref Unknown,ref Unknown);
}
}
}