批处理模板更改(Microsoft Word)
模板用于快速定义一个文档或一组文档的标准外观。关于模板的妙处在于,您可以定义一个模板使文档具有一种外观,而另一个模板则赋予其完全不同的外观。当然,您需要做的就是更改与文档关联的模板。
更改与一个或两个文档关联的模板非常容易。如果您的目录中充满了需要更改其模板的文档,该怎么办?这很快就会变得很乏味。这是宏可以进行救援的地方,以释放旧的乏味并快速地处理日常事务。下面的宏ChangeTemplates修改特定目录中的所有文档,以确保它们使用所需的模板。
Sub ChangeTemplates() Dim strDocPath As String Dim strTemplateB As String Dim strCurDoc As String Dim docCurDoc As Document ' set document folder path and template strings strDocPath = "C:\path to document folder\" strTemplateB = "C:\path to template\templateB.dot" ' get first doc - only time need to provide file spec strCurDoc = Dir(strDocPath & "*.doc") ' ready to loop (for as long as file found) Do While strCurDoc <> "" ' open file Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc) ' change the template docCurDoc.AttachedTemplate = strTemplateB ' save and close docCurDoc.Close wdSaveChanges ' get next file name strCurDoc = Dir Loop MsgBox "Finished" End Sub
为了使用宏,只需确保正确设置了strDocPath和strTemplateB变量。宏更改特定目录中所有文档的模板关联。如果您需要更多区分性的内容,那么可以使用另一个宏。例如,您可能希望宏检查每个文档,而仅更改那些使用TemplateA的文档,以便它们现在使用TemplateB。在这种情况下,您会发现以下宏非常方便:
Sub TemplateBatchChange() Dim objPropertyReader Dim strFolder As String Dim strFileName As String Dim objThisDoc As Word.Document Dim strFindTemplate As String Dim strReplaceTemplate As String Dim strAffectedDocs As String On Error Resume Next 'Create the PropertyReader object Set objPropertyReader = CreateObject("DSOleFile.PropertyReader") If Err.Number <> 0 Then MsgBox "You must install the DSOleFile component. See " & _ "http://support.microsoft.com/kb/224351" GoTo FinishUp End If 'Get the template names strFindTemplate = UCase(InputBox("Name of template to find (exclude the .dot)") & _ ".dot") strReplaceTemplate = InputBox("Name of replacement template (exclude the .dot)") & _ ".dot" 'Make sure it's a valid template. Try to create a new document based on it. Set objThisDoc = Word.Documents.Add(strReplaceTemplate, Visible:=False) If Err.Number <> 0 Then 'No such template MsgBox "There is no accessible template named " & strReplaceTemplate GoTo FinishUp End If 'Close the test document objThisDoc.Close wdDoNotSaveChanges On Error GoTo ErrorHandler 'Get the current documents path strFolder = Word.Application.Options.DefaultFilePath(wdDocumentsPath) _ & Word.Application.PathSeparator 'Examine all Word documents in the directory 'Get the first document name strFileName = Dir(strFolder & "*.doc") While strFileName <> "" 'Look at the template name If UCase(objPropertyReader.GetDocumentProperties _ (strFolder & strFileName).Template) = strFindTemplate Then 'It matches. Open the document Set objThisDoc = Word.Documents.Open _ (FileName:=strFileName, Visible:=False) 'Change the attached template objThisDoc.AttachedTemplate = strReplaceTemplate 'Save the change objThisDoc.Close wdSaveChanges 'Note the document strAffectedDocs = strAffectedDocs & strFileName & ", " End If 'Get the next document strFileName = Dir Wend 'Report the results If strAffectedDocs = "" Then MsgBox "No documents were changed.", , "Template Batch Change" Else 'Remove the trailing comma and space strAffectedDocs = Left(strAffectedDocs, Len(strAffectedDocs) - 2) MsgBox "These documents were changed: " & _ strAffectedDocs, , "Template Batch Change" End If GoTo FinishUp ErrorHandler: Set objThisDoc = Nothing Set objPropertyReader = Nothing Err.Raise vbError + 1001, "TemplateBatchChange", _ "TemplateBatchChange encountered an error: " & Err.Description FinishUp: 'Release object references Set objThisDoc = Nothing Set objPropertyReader = Nothing End Sub
为了使用此宏,必须首先确保在系统上安装了DSOLEFILE组件(可从Microsoft免费获得)。
您可以通过参考Microsoft的224351知识库文章找到有关此组件的更多信息。宏为您提供了指定要替换的模板名称和替换模板的名称的机会。它甚至检查以确保替换模板存在。
此宏在默认的Word文档文件夹中搜索文档。
如果要直接在其他目录中搜索,则应确保将strFolder变量设置为要使用的文件夹的完整路径。
注意:
如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)上描述的宏,我准备了一个包含有用信息的特殊页面。
_WordTips_是您进行经济有效的Microsoft Word培训的来源。
(Microsoft Word是世界上最流行的文字处理软件。)本技巧(1437)适用于Microsoft Word 97、2000、2002和2003。您可以在Word(Word 2007)的功能区界面中找到此技巧的版本。和更高版本)在这里: