Word Interop-添加新的自动文本/构建块

时间:2016-09-07 14:11:51

标签: c# ms-word office-interop com-interop

我需要更新当前存储在多个word文档中的一堆值作为自动文本(或构建块),有太多的手工操作,所以我希望使用Interop Word API。

var app = new Application();
var doc = app.Documents.Open(@"c:\path\to\file.dot");

很遗憾,我看不到Document中与Word中的自动文字功能相关的任何成员(插入>快速部件> Building Blocks Organizer)。

API是否公开了在“Building Blocks Organizer”中添加/更新自动文本值的任何方式?

1 个答案:

答案 0 :(得分:1)

您需要做的是创建一个新文档并将模板附加到该文档中,从头顶开始:

ActiveDocument.AttachedTemplate = @" C:\ path \ to \ file.dot";

之后你就可以像这样对待AutoTextEntries(一个VBA示例,但我确定你可以自己快速将它重写为C#)

Sub test()

    ActiveDocument.AttachedTemplate = @"C:\path\to\file.dot"

    For Each oAutoText In ActiveDocument.AttachedTemplate.AutoTextEntries
        MsgBox oAutoText.Value
        oAutoText.Value = Replace(oAutoText.Value, strOld, strNew)
    Next oAutoText

End Sub