我正在尝试使用Word Interop尝试将Word文档中的TAG替换为另一个文档的内容。
例如,我必须处理example.doc
,其标记为#TAG#123456789#
,然后我必须复制123456789.doc
并将内容粘贴到example.doc
替换标记#TAG #123456789#
文档123456789.doc
有其表格,图像等等。
到目前为止,我可以在example.doc中找到标签并获取123456789.doc
但我找不到除文本替换之外的任何方法的工作方法。
我正在考虑一种新方法,通过使用以下范围附加部分文档来生成新文档:
example.doc
。123456789.doc
example.doc
的其余部分附加到文档的末尾。答案 0 :(得分:0)
看起来你已接近解决方案了。找到标记并在文档中选择它时,可以使用WordApp.Selection.InsertFile()方法将其他文档中的内容插入到该选择中。
确保首先进行选择非常重要。
示例方法:
/// <summary>
/// This method adds a file to the Word.ApplicationClass object's current selection.
/// Has been tested with word (*.doc)documents but not with other office files.
/// Other files may work also with this method.
/// Tested with the Microsoft 9.0 Object Library ( COM )
/// </summary>
/// <param name="WordApp">The Word.ApplicationClass object</param>
/// <param name="DirLocation">A string that contains the file directory location.</param>
/// <param name="FileName">A string that contains the file name within the directory location.</param>
public static void InsertFile(Word.ApplicationClass WordApp, string DirLocation, string FileName)
{
try
{
object j_NullObject = System.Reflection.Missing.Value;
WordApp.Selection.InsertFile(DirLocation + FileName, ref j_NullObject, ref j_NullObject, ref j_NullObject, ref j_NullObject);
}
//An Exception will occur when the Directory location and filename does not exist.
//An Exception may also occur when the Word.ApplicationClass has no Selection to add
//the file to.
catch (Exception e)
{
//show the user the error message
MessageBox.Show(e.Message);
//log the error
//ErrorLog.LogError(e);
}
}