MS Word 文件样式随外部文档自动更新

MS Word 文件样式随外部文档自动更新

在 MS Word 中我们有一个模板文档 (模板1.docm).此文档在SharePoint中被很多用户用来编写数以千计的流程文档,并保存为.mht文档。

当我们更新模板中的样式时,我们想更新从该模板创建的所有其他文档。

是否可以只更新这一模板文件中的样式并让所有其他文件自动更新?

如果没有,我还能做什么来使用模板文件的更改来更新这数千份文档?

答案1

我将尝试概述使用 VBA 宏解决该问题的方法,但请注意,这完全是理论上的且未经测试。

这个想法是创建一个包含 VBA 宏的工作文档,该宏在请求文件夹后,将模板重新附加到文件夹中的所有文档。

不要将包含此宏的文件放在要处理的文件夹中,否则它将自行处理。

这是宏:

Sub UpdateDocuments()
Dim strFolder As String, strFile As String, strCurDoc As Document, strTemplate As String
strFolder = GetFolder
strTemplate = "C:\path to template\template.dot"
If strFolder = "" Then Exit Sub
Application.ScreenUpdating = False
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  Set strCurDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  strCurDoc.AttachedTemplate = strTemplate
  strCurDoc.Close wdSaveChanges
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

相关内容