Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. 'main function'
  2. Public Sub protectSheet()
  3. ActiveWorkbook.ActiveSheet.protect Password:="password", AllowUsingPivotTables:=True
  4. End Sub
  5.  
  6. Public Sub ShowToolbar()
  7. ' Assumes toolbar not already loaded '
  8. Application.CommandBars.Add Module1.TOOLBARNAME
  9. AddButton "Proteksi Sheet", "Memproteksi Pivot", 3817, "protectSheet"
  10.  
  11. ' call AddButton more times for more buttons '
  12. With Application.CommandBars(Module1.TOOLBARNAME)
  13. .Visible = True
  14. .Position = msoBarTop
  15. End With
  16. End Sub
  17.  
  18. Public Sub AddButton(caption As String, tooltip As String, faceId As Long, methodName As String)
  19. Dim Btn As CommandBarButton
  20. Set Btn = Application.CommandBars(Module1.TOOLBARNAME).Controls.Add
  21. With Btn
  22. .Style = msoButtonIcon
  23. .faceId = faceId ' choose from a world of possible images in Excel: see http://www.ozgrid.com/forum/showthread.php?t=39992 '
  24. .OnAction = methodName
  25. .TooltipText = tooltip
  26. End With
  27. End Sub
  28.  
  29. Public Sub DeleteCommandBar()
  30. Application.CommandBars(TOOLBARNAME).Delete
  31. End Sub
  32.  
  33.  
  34.  
  35. 'called when add-in installed
  36. Private Sub Workbook_AddinInstall()
  37. Call ShowToolbar
  38. End Sub
  39.  
  40. 'called when add-in uninstalled
  41. Private Sub Workbook_AddinUninstall()
  42. Call DeleteCommandBar
  43. End Sub
  44.  
  45. Public Const TOOLBARNAME = "PivotTools"
  46.  
  47. Public Const TOOLBARNAME = "PivotTools"
  48.  
  49. 'caller method'
  50. Public Sub protectDoc()
  51.  
  52.  
  53. On Error GoTo errorInfo
  54. protectSheet
  55.  
  56. 'if success, show msg box'
  57. MsgBox ("Report berhasil diproteksi")
  58.  
  59. Exit Sub
  60.  
  61. errorInfo:
  62. MsgBox Err.Description & vbCrLf & Err.Number
  63.  
  64. End Sub
  65.  
  66.  
  67. Public Sub protectSheet()
  68. ActiveWorkbook.ActiveSheet.protect Password:="password", AllowUsingPivotTables:=True
  69. End Sub
  70.  
  71. Public Sub refreshDoc()
  72.  
  73. On Error GoTo errorInfo
  74.  
  75. refreshConnection
  76. protectSheet
  77. 'if success, show msg box'
  78. MsgBox ("Report berhasil diperbaharui")
  79.  
  80. Exit Sub
  81.  
  82. errorInfo:
  83. MsgBox Err.Description & vbCrLf & Err.Number
  84.  
  85. End Sub
  86.  
  87.  
  88. Private Sub refreshConnection()
  89. ActiveWorkbook.ActiveSheet.Unprotect Password:="password"
  90. ActiveWorkbook.RefreshAll
  91. End Sub
  92.  
  93.  
  94. Public Sub ShowToolbar()
  95. ' Assumes toolbar not already loaded '
  96. Application.CommandBars.Add TOOLBARNAME
  97. AddButton "Proteksi Sheet", "Memproteksi Pivot", 225, "protectDoc"
  98. AddButton "Refresh Data", "Refresh Pivot", 459, "refreshDoc"
  99.  
  100. ' call AddButton more times for more buttons '
  101. With Application.CommandBars(TOOLBARNAME)
  102. .Visible = True
  103. .Position = msoBarTop
  104. End With
  105. End Sub
  106.  
  107. Public Sub AddButton(caption As String, tooltip As String, faceId As Long, methodName As String)
  108. Dim Btn As CommandBarButton
  109. Set Btn = Application.CommandBars(TOOLBARNAME).Controls.Add
  110. With Btn
  111. .Style = msoButtonIcon
  112. .faceId = faceId
  113. ' choose from a world of possible images in Excel: see http://www.ozgrid.com/forum/showthread.php?t=39992 '
  114. .OnAction = methodName
  115. .TooltipText = tooltip
  116. End With
  117. End Sub
  118.  
  119. Public Sub DeleteCommandBar()
  120. Application.CommandBars(TOOLBARNAME).Delete
  121. End Sub
  122.  
  123. 'called when add-in installed
  124. Private Sub Workbook_AddinInstall()
  125. Call Module1.ShowToolbar
  126. End Sub
  127.  
  128. 'called when add-in uninstalled
  129. Private Sub Workbook_AddinUninstall()
  130. Call Module1.DeleteCommandBar
  131. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement