Advertisement
Guest User

Untitled

a guest
May 29th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. Option Explicit
  2.  
  3. ' CC BY-SA
  4. ' by Gee Law on github.com/geelaw
  5.  
  6. Sub DetachTemplate()
  7. '
  8. ' DetachTemplate Macro
  9. '
  10. '
  11. If ActiveDocument.AttachedTemplate = NormalTemplate Then
  12.  
  13. MsgBox "The document is already attached to the normal template.", vbInformation, "Already attached"
  14. Exit Sub
  15.  
  16. End If
  17.  
  18. If MsgBox("Do you want to attach the normal template to this document?", vbQuestion + vbYesNo + vbDefaultButton2, "Detaching custom template") = vbYes Then
  19.  
  20. On Error GoTo NotAnExpectedAction
  21. ActiveDocument.Styles.Item ("Detach Template Tip")
  22.  
  23. On Error GoTo AttachTemplateFailed
  24. ActiveDocument.AttachedTemplate = NormalTemplate
  25.  
  26. On Error GoTo CleanupFailed
  27. With ActiveDocument.Content.Find
  28. .Style = "Detach Template Tip"
  29. .Wrap = wdFindContinue
  30. With .Replacement
  31. .Text = ""
  32. End With
  33. .Execute Replace:=wdReplaceAll
  34. End With
  35. ActiveDocument.Styles("Detach Template Tip").Delete
  36.  
  37. MsgBox "Successfully attached the normal template to this document.", vbInformation, "Successful operation"
  38.  
  39. Exit Sub
  40.  
  41. NotAnExpectedAction:
  42. MsgBox "This file is not supported." & vbCrLf & "The document is not reattached to Normal.dotm." _
  43. & vbCrLf & vbCrLf & "Only files created from a template with Detach Template Tip style can be removed.", _
  44. vbExclamation, "Not supported"
  45. Exit Sub
  46.  
  47. AttachTemplateFailed:
  48. MsgBox "Couldn't attach the normal template to this document.", vbCritical, "Error"
  49. Exit Sub
  50.  
  51. CleanupFailed:
  52. 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"
  53. Exit Sub
  54.  
  55. End If
  56.  
  57. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement