Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement