最近一個項目的需求是要根據一個Word文檔的模板,用記錄集的具體內容替換掉裡面的標識字符的內容,生成不同的文檔。 分兩步:
第一:復制模板的內容到一個Document對象裡 從源DOC文檔復制內容返回一個Document類#region 從源DOC文檔復制內容返回一個Document類
/// <summary>
/// 從源DOC文檔復制內容返回一個Document類
/// </summary>
/// <param name="sorceDocPath">源DOC文檔路徑</param>
/// <returns>Document</returns>
protected Document copyWordDoc(object sorceDocPath)
{
object objDocType = WdDocumentType.wdTypeDocument;
object type = WdBreakType.wdSectionBreakContinuous;
//Word應用程序變量
Application wordApp;
//Word文檔變量
Document newWordDoc;
object readOnly = false;
object isVisible = false;
//初始化
//由於使用的是COM庫,因此有許多變量需要用Missing.Value代替
wordApp = new ApplicationClass();
Object Nothing = System.Reflection.Missing.Value;
//wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
Document openWord;
openWord = wordApp.Documents.Open(ref sorceDocPath, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing,