
/**//********************************************************************
created: 2007/11/02
created: 2:11:2007 23:13
filename: D:C#程序練習WordToChmWordToHtml.cs
file path: D:C#程序練習WordToChm
file base: WordToHtml
file ext: cs
author: 凌劍 Bujiwu
purpose: 將Word文件轉化為Html文件
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace CreateWordToHtmlFileSpace
...{
class WordToHtml
...{
public static void CreateWordToHtmlFile(string WordFileDir)
...{
DealWithWordFile(WordFileDir);
}
//搜索WordFileDir在的*.doc文件
private static void DealWithWordFile(string WordFileDir)
...{
//創建數組保存源文件夾下的文件名
string[] strFiles = Directory.GetFiles(WordFileDir, "*.doc");
for (int i = 0; i < strFiles.Length; i++)
...{
WordToHtmlFile(strFiles[i]);
}
DirectoryInfo dirInfo = new DirectoryInfo(WordFileDir);
//取得源文件夾下的所有子文件夾名稱
DirectoryInfo[] ZiPath = dirInfo.GetDirectorIEs();
for (int j = 0; j < ZiPath.Length; j++)
...{
//獲取所有子文件夾名
string strZiPath = WordFileDir + "\" + ZiPath[j].ToString();
//把得到的子文件夾當成新的源文件夾,從頭開始新一輪的搜索
DealWithWordFile(strZiPath);
}
}
//轉化
private static void WordToHtmlFile(string WordFilePath)
...{
try
...{
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
// 指定原文件和目標文件
object Source = WordFilePath;
string SaveHtmlPath = WordFilePath.Substring(0, WordFilePath.Length - 3) + "Html";
object Target = SaveHtmlPath;
// 缺省參數
object Unknown = Type.Missing;
//為了保險,只讀方式打開
object readOnly = true;
// 打開doc文件
Microsoft.Office.Interop.Word.Document doc
= newApp.Documents.Open(ref Source, ref Unknown,
ref readOnly, 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);
// 指定另存為格式(rtf)
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHtml;
// 轉換格式
doc.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);
// 關閉文檔和Word程序
doc.Close(ref Unknown, ref Unknown, ref Unknown);
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
}
catch(Exception e)
...{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
}
}