【问题标题】:Word Interop- Add new autotext/building blockWord 互操作 - 添加新的自动文本/构建块
【发布时间】:2016-09-07 14:11:51
【问题描述】:

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

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

很遗憾,我看不到与 Word 中的自动文本功能(插入 > 快速部件 > 构建块管理器)相关的 Document 的任何成员。

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 正是我想要的谢谢。不幸的是,我正在使用的文档被滥用,这意味着我实际上无法以编程方式更新自动图文集,但确实有一个工作版本。
    猜你喜欢
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多