我有一个 DateTable
。需要将其插入到模板文档中的某些字符集合中。
我可以通过文本替换的方式来实现(使用大量的missingObj来避免错误):
using Word = Microsoft.Office.Interop.Word;
Word._Application application;
Word._Document document;
Object missingObj = System.Reflection.Missing.Value;
Object trueObj = true;
Object falseObj = false;
private void create_button1_Click(object sender, EventArgs e) {
application = new Word.Application();
Object templatePathObj;
templatePathObj = "template.dot";
try {
document = application.Documents.Add(ref templatePathObj,
ref missingObj, ref missingObj, ref missingObj);
}
catch (Exception error) {
document.Close(ref falseObj, ref missingObj, ref missingObj);
application.Quit(ref missingObj, ref missingObj, ref missingObj);
document = null;
application = null;
throw error;
}
object strToFindObj = "%%mark%%";
object replaceStrObj = "text to replace";
Word.Range wordRange;
object replaceTypeObj;
replaceTypeObj = Word.WdReplace.wdReplaceAll;
for (int i = 1; i <= document.Sections.Count; i++) {
wordRange = document.Sections[i].Range;
Word.Find wordFindObj = wordRange.Find;
object[] wordFindParameters = new object[15] { strToFindObj, missingObj,
missingObj, missingObj, missingObj, missingObj, missingObj, missingObj,
missingObj, replaceStrObj, replaceTypeObj, missingObj, missingObj,
missingObj, missingObj };
wordFindObj.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod,
null, wordFindObj, wordFindParameters);
}
application.Visible = true;
}
我需要改变什么,使得这段代码可以使用DataTable
而不是strToFindObj
?
在这个例子中,我替换了Range
中的内容,这些内容是文档的一部分,包括表格、格式等。