템플릿은 문서 또는 문서 그룹의 표준 모양을 빠르게 정의하는 데 사용됩니다. 템플릿의 멋진 점은 하나의 템플릿을 정의하여 문서에 하나의 모양을 부여하고 다른 템플릿은 완전히 다른 모양을 제공 할 수 있다는 것입니다. 물론 문서와 관련된 템플릿을 변경하기 만하면됩니다.

하나 또는 두 개의 문서와 관련된 템플릿을 변경하는 것은 매우 쉽습니다. 템플릿을 변경해야하는 문서로 가득 찬 디렉토리가 있다면 어떨까요? 이것은 매우 빠르게 지루해질 수 있습니다. 여기에서 매크로가 구출 될 수 있습니다. 오래된 지루함을 덜어주고 평범한 일을 매우 빠르게 처리 할 수 ​​있습니다. 다음 매크로 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.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

매크로를 사용하려면 strDocPath 및 strTemplateB 변수가 올바르게 설정되었는지 확인하기 만하면됩니다. 매크로는 특정 디렉토리의 모든 문서에 대한 템플릿 연결을 변경합니다. 좀 더 차별적 인 것을 원한다면 다른 매크로가 필요합니다. 예를 들어, 매크로가 각 문서를 검사하고 이제 TemplateB를 사용하도록 TemplateA를 사용하는 문서 만 변경하도록 할 수 있습니다. 이 경우 다음 매크로가 매우 편리합니다.

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

이 매크로를 사용하려면 먼저 DSOLEFILE 구성 요소 (Microsoft에서 무료로 제공)가 시스템에 설치되어 있는지 확인해야합니다.

이 구성 요소에 대한 자세한 내용은 Microsoft의 224351 기술 자료 문서를 참조하십시오. 매크로는 교체 할 템플릿 이름과 교체 할 템플릿 이름을 지정할 수있는 기회를 제공합니다. 대체 템플릿이 있는지 확인합니다.

이 매크로는 기본 Word 문서 폴더에서 문서를 검색합니다.

다른 곳에서 직접 검색하려면 strFolder 변수가 사용하려는 폴더의 전체 경로로 설정되어 있는지 확인해야합니다.

_ 참고 : _

이 페이지 (또는 WordTips 사이트의 다른 페이지)에 설명 된 매크로를 사용하는 방법을 알고 싶다면 유용한 정보가 포함 된 특별 페이지를 준비했습니다.

link : / wordribbon-WordTipsMacros [새 브라우저 탭에서 특별 페이지를 열려면 여기를 클릭하세요].

_WordTips_는 비용 효율적인 Microsoft Word 교육을위한 소스입니다.

(Microsoft Word는 세계에서 가장 인기있는 워드 프로세싱 소프트웨어입니다.)이 팁 (10338)은 Microsoft Word 2007, 2010, 2013 및 2016에 적용됩니다. 여기에서 Word의 이전 메뉴 인터페이스에 대한이 팁 버전을 찾을 수 있습니다. link : / word-Batch_Template_Changes [Batch Template Changes].