Low Security and this runs, Medium it doesn't - Why?

M

MarkAtlanta

code:
-----------------------------------------------------------
'At top of this module
Dim MyBar As CommandBar
Dim MadeButton As CommandBarButton
Dim Page1Button As CommandBarButton
Dim Page2Button As CommandBarButton
Dim Page3Button As CommandBarButton
Dim Sheetname As String

Sub CustomToolbar
'code to set up toolbar and assign other buttons
MakeButton ("Page1")
MakeButton ("Page2")
MakeButton ("Page3")
'other code
End sub

Sub MakeButton(Sheetname)
Set MadeButton = MyBar.Controls.Add
(Type:=msoControlButton)
With MadeButton
.Style = msoButtonIconAndCaption
If Worksheets(Sheetname).Visible <> -1 Then
.Caption = "Show '" & Sheetname & "'"
.FaceId = 170
.State = msoButtonUp
Else
.Caption = "Hide '" & Sheetname & "'"
.FaceId = 171
.State = msoButtonDown
End If
.OnAction = "'HideShow """ & Sheetname & """'"
End With
End Sub

Sub HideShow(Sheetname)
If Worksheets(Sheetname).Visible = -1 Then
Worksheets(Sheetname).Visible = 2
Else: Worksheets(Sheetname).Visible = -1
Worksheets(Sheetname).Select
End If
FinishToolBar
End Sub
-----------------------------------------------------------
When a user has their Tools, Macro, Security, Security
Levels, set to Low, this Macro runs just fine. However
when they have it set to Medium (and they Enable Macros),
they can click on one of these buttons, but no sheets are
made visible or hidden. Trying to figure it out, when I
put MsgBox Sheetname as the first line of HideShow, it
never pops up. So it looks like the .onaction event is
disabled by the medium security level setting. Does this
make any sense? I know how to get around this (hard code
the buttons instead using loops and variables, or I can
require that the users set their Security to Low) so that
isn't my question. My question is what do I do to get
this work, or same thing is different between Low and
Medium-Enable. Thank you all so much!
 
M

MarkAtlanta

The code in my first post was not as complete as it could
have been. If anyone wanted to load the code and test it,
I wanted it as perfect as possible.

'At top of this module
Dim MyBar As CommandBar
Dim MadeButton As CommandBarButton
Dim Page1Button As CommandBarButton
Dim Page2Button As CommandBarButton
Dim Page3Button As CommandBarButton
Dim Sheetname As String

Sub DeleteToolBar() 'I didn't show this macro in my
original post
On Local Error Resume Next
MyBar.Delete
On Local Error GoTo 0
End Sub

Sub CustomToolbar
DeleteToolBar 'I didn't show this line in my
original post
Set MyBar = CommandBars.Add("CustomToolbar",
msoBarFloating, , Temporary:=True) 'I didn't show this
line in my original post
MakeButton ("Page1")
MakeButton ("Page2")
MakeButton ("Page3")
'other code
With MyBar 'This 'With MyBar' was not shown in my
original post
.Protection = msoBarNoChangeVisible +
msoBarNoResize
.Width = 150
.Visible = True
.Top = 600
.Left = 700
End With
End sub

Sub MakeButton(Sheetname)
Set MadeButton = MyBar.Controls.Add
(Type:=msoControlButton)
With MadeButton
.Style = msoButtonIconAndCaption
If Worksheets(Sheetname).Visible <> -1 Then
.Caption = "Show '" & Sheetname & "'"
.FaceId = 170
.State = msoButtonUp
Else
.Caption = "Hide '" & Sheetname & "'"
.FaceId = 171
.State = msoButtonDown
End If
.OnAction = "'HideShow """ & Sheetname & """'"
End With
End Sub

Sub HideShow(Sheetname)
If Worksheets(Sheetname).Visible = -1 Then
Worksheets(Sheetname).Visible = 2
Else: Worksheets(Sheetname).Visible = -1
Worksheets(Sheetname).Select
End If
CustomToolBar 'my original posting had FinishToolBar
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top