Mẫu được sử dụng để nhanh chóng xác định giao diện chuẩn cho một tài liệu hoặc một nhóm tài liệu. Điều tuyệt vời về các mẫu là bạn có thể xác định một mẫu để cung cấp cho tài liệu của bạn một giao diện và một mẫu khác để tạo cho nó một giao diện hoàn toàn khác. Tất nhiên, tất cả những gì bạn cần làm là thay đổi mẫu nào được liên kết với tài liệu.

Thay đổi mẫu được liên kết với một hoặc hai tài liệu khá dễ dàng. Điều gì sẽ xảy ra nếu bạn có một thư mục chứa đầy tài liệu cần thay đổi mẫu? Điều này có thể trở nên khá tẻ nhạt rất nhanh chóng. Đây là nơi mà một macro có thể đến để giải cứu — để giải tỏa sự tẻ nhạt cũ kỹ đó và thực hiện công việc trần tục rất nhanh chóng. Macro sau, ChangeTemplates, sửa đổi tất cả các tài liệu trong một thư mục cụ thể để đảm bảo chúng sử dụng mẫu bạn muốn.

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.dotx"



' 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

Để sử dụng macro, chỉ cần đảm bảo rằng các biến strDocPath và strTemplateB được đặt đúng cách. Macro thay đổi các liên kết mẫu cho tất cả các tài liệu trong một thư mục cụ thể. Nếu bạn muốn một cái gì đó phân biệt hơn một chút, thì một macro khác theo thứ tự. Ví dụ: bạn có thể muốn macro kiểm tra từng tài liệu và chỉ thay đổi những tài liệu sử dụng TemplateA để bây giờ chúng sử dụng TemplateB. Trong trường hợp này, bạn sẽ thấy macro sau rất tiện dụng:

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/?kbid=224351"

GoTo FinishUp     End If

'Get the template names     strFindTemplate = UCase(InputBox("Name of template to find"))

7     strReplaceTemplate = InputBox("Name of replacement template")



'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

Để sử dụng macro này, trước tiên bạn phải đảm bảo rằng thành phần DSOLEFILE (có sẵn miễn phí từ Microsoft) được cài đặt trên hệ thống của bạn.

Bạn có thể tìm hiểu thêm về thành phần này bằng cách tham khảo bài viết trên Cơ sở Kiến thức 224351 của Microsoft. Macro cung cấp cho bạn cơ hội chỉ định tên mẫu sẽ được thay thế và tên của mẫu để thay thế bằng. Nó thậm chí còn kiểm tra để đảm bảo rằng mẫu thay thế tồn tại.

Macro này tìm kiếm tài liệu trong thư mục tài liệu Word mặc định.

Nếu bạn muốn trực tiếp tìm kiếm trong một khác, bạn nên đảm bảo rằng biến strFolder được đặt thành đường dẫn đầy đủ của thư mục bạn muốn sử dụng.

_Lưu ý: _

Nếu bạn muốn biết cách sử dụng các macro được mô tả trên trang này (hoặc trên bất kỳ trang nào khác trên các trang WordTips), tôi đã chuẩn bị một trang đặc biệt bao gồm thông tin hữu ích.

WordTips là nguồn của bạn để đào tạo Microsoft Word hiệu quả về chi phí.