转一个CodeProject上的文章,原文代码有问题,修改编译后通过。
原文在这里
先在项目引用里添加上对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);
}
}
}
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);
}
}
}