要寫一個簡單的程式去WORD去SEARCH一些文字方塊中的特定字串並取代新字串,再印出來
簡單的範例如下..
記得先參考
Microsoft Word 12.0 Object library或Microsoft Word 13.0 Object library
或Microsoft Word 14.0 Object library(對映office版本)
using System.IO;
using System.Data.OleDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Threading;
/* 中略 */
#region 取代WORD主程式
#region 取代文字FUNCTION(被呼叫的FUN)
public void exchange(string oldstring, string newstring, ref Word._Document oDoc,ref
Word._Application oWord)
{
#region 文字區域
string oldStr = oldstring;//被取代的文字
string newStr = newstring;//要放入的文字
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
oWord.Selection.Find.ClearFormatting();
oWord.Selection.Find.Text = oldStr;
oWord.Selection.Find.Replacement.ClearFormatting();
oWord.Selection.Find.Replacement.Text = newStr;
oWord.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
#endregion
#region 文本框
StoryRanges sr = oDoc.StoryRanges;
foreach (Range r in sr)
{
Range r1 = r;
if (WdStoryType.wdTextFrameStory == r.StoryType)
{
do
{
r1.Find.ClearFormatting();
r1.Find.Text = oldStr;
r1.Find.Replacement.ClearFormatting();
r1.Find.Replacement.Text = newStr;
r1.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
r1 = r1.NextStoryRange;
} while (r1 != null);
}
}
#endregion
}
#endregion
//此為主程式
public void change_wordDOC(System.Data.DataTable dt,int NUM,string filenameDoc,
string ADDRESS)
{
copy_doc(filenameDoc);//此為將要取代的文件複製到另一個資料夾再處理,省略內容..
string[] str_list = new string[] { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG",
"HHH" };//比對WORD位置
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = false;//背景處理不開啟word介面
// 打開文件:
string filename_root = System.Windows.Forms.Application.StartupPath + @"\DOC2\"
+ filenameDoc;
object fileName = System.Windows.Forms.Application.StartupPath + @"\DOC2\"
+ filenameDoc;
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
for (int i = 0; i < NUM; i++)
{
exchange(str_list[i].ToString(), dt.Rows[0][i].ToString(), ref oDoc, ref oWord);
//呼叫取代字串的函數 exchange(要被取代的字串,要取代的字串,
開啟Word._Document ,開啟的Word._Application)
}
exchange("ADDRESS", ADDRESS, ref oDoc, ref oWord);
//oDoc.PrintPreview();//可以顯示word預覽列印
oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Thread.Sleep(3000);
MessageBox.Show("列印完成");
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);//關閉文件
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);//關閉WORD
}
#endregion