2016-09-07 22 views
0

我需要更新當前存儲在多個Word文檔的自動文本(或構件塊)一羣值的,有太多的手工做的,所以我希望能使用Interop Word API。字Interop-添加新的自動圖文集/積木

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

可惜我看不到在Word中(插入>快速部件>構建基塊管理)相關的自動文本功能Document任何成員。

API是否公開將/在「構建基塊管理」自動更新文本值的方法嗎?

回答

1

你需要做的就是創建一個新的文件和模板附加到文件,從我的頭頂:

ActiveDocument.AttachedTemplate = @「C:\路徑\爲\ file.dot」 ;

之後,你可以在interate像這樣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 
+0

正是我一直在尋找的感謝。不幸的是,我正在使用的文檔已被誤用,這意味着我無法以編程方式實際更新自動圖文集,但確實有工作版本。 – Anth12