Skip to content

Instantly share code, notes, and snippets.

@GeeLaw
Last active May 10, 2022 16:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeeLaw/d21b63434e72a34ba62e to your computer and use it in GitHub Desktop.
Save GeeLaw/d21b63434e72a34ba62e to your computer and use it in GitHub Desktop.
A VBA Macro for Microsoft Word to attach the normal template (Normal.dotm) to the active document.
Option Explicit
' CC BY-SA
' by Gee Law on github.com/geelaw
Sub DetachTemplate()
'
' DetachTemplate Macro
'
'
If ActiveDocument.AttachedTemplate = NormalTemplate Then
MsgBox "The document is already attached to the normal template.", vbInformation, "Already attached"
Exit Sub
End If
If MsgBox("Do you want to attach the normal template to this document?", vbQuestion + vbYesNo + vbDefaultButton2, "Detaching custom template") = vbYes Then
On Error GoTo NotAnExpectedAction
ActiveDocument.Styles.Item ("Detach Template Tip")
On Error GoTo AttachTemplateFailed
ActiveDocument.AttachedTemplate = NormalTemplate
On Error GoTo CleanupFailed
With ActiveDocument.Content.Find
.Style = "Detach Template Tip"
.Wrap = wdFindContinue
With .Replacement
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Styles("Detach Template Tip").Delete
MsgBox "Successfully attached the normal template to this document.", vbInformation, "Successful operation"
Exit Sub
NotAnExpectedAction:
MsgBox "This file is not supported." & vbCrLf & "The document is not reattached to Normal.dotm." _
& vbCrLf & vbCrLf & "Only files created from a template with Detach Template Tip style can be removed.", _
vbExclamation, "Not supported"
Exit Sub
AttachTemplateFailed:
MsgBox "Couldn't attach the normal template to this document.", vbCritical, "Error"
Exit Sub
CleanupFailed:
MsgBox "Could not clean up some of the tips notifying you to detach the template. You might have to clean them up yourself.", vbExclamation, "Cleanup failed"
Exit Sub
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment