About Me
Seguidores
Estatisticas
180: VBE - ToolBar personalizado
Se pretendermos construir um ToolBar personalizado (a que chamaremos "MyToolBar"), desactivando outros ToolBars existentes, deixando apenas activo o ToolBar denominado "WorkSheet Menu Bar" e o MyToolBar e ainda que este seja apagado quando saímos do workbook, reactivando todos os outros ToolBars e, por fim, que, quando de novo abrirmos o workbook, voltemos a ter o MyToolBar disponível, podemos utilizar as seguintes peças de código, para o exemplo que se apresenta:
Código num módulo:
Sub MakeToolBar()
On Error Resume Next
Application.CommandBars("MyToolBar").Delete
On Error GoTo 0
With Application.CommandBars.Add(Name:="MyToolBar", _
Position:=msoBarTop, MenuBar:=False)
.Protection = msoBarNoCustomize
With .Controls.Add(Type:=msoControlButton)
.Style = msoButtonCaption
.DescriptionText = "Imprime, Grava e Sai"
.TooltipText = "Imprime, Grava e Sai"
.Caption = "Imprime/Grava/Sai"
.OnAction = "Imprime_Grava"
End With
With .Controls.Add(Type:=msoControlButton)
.Style = msoButtonCaption
.DescriptionText = "Limpar os Dados Anteriores"
.TooltipText = "Limpa os Dados Anteriores"
.Caption = "Limpa Dados"
.OnAction = "Limpa"
End With
With .Controls.Add(Type:=msoControlButton)
.Style = msoButtonCaption
.DescriptionText = "Gravar o Novo Mês"
.TooltipText = "Grava o Novo Mês"
.Caption = "Novo Mês"
.OnAction = "GravaSai"
End With
With .Controls.Add(Type:=msoControlButton)
.Style = msoButtonCaption
.DescriptionText = "Inserção do valor que vem do fecho do mês anterior"
.TooltipText = "Digitar o Valor do Mês Anterior"
.Caption = "Valor Mês Anterior"
.OnAction = "Valor_Anterior"
End With
Application.CommandBars("MyToolBar").Visible = True
End With
End Sub
Sub DeleteToolBar()
Dim bar As CommandBar
On Error Resume Next
Application.CommandBars("MyToolBar").Delete
On Error GoTo 0
End Sub
Código no Workbook:
Private Sub Workbook_Activate()
On Error Resume Next
With Application.CommandBars("Worksheet Menu Bar")
.Enabled = True
.Visible = True
End With
With Application.CommandBars("Formatting")
.Enabled = False
.Visible = False
End With
With Application.CommandBars("Standard")
.Enabled = False
.Visible = False
End With
With Application.CommandBars("TranslateIT")
.Enabled = False
.Visible = False
End With
MakeToolBar
On Error GoTo 0
End Sub
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application.CommandBars("Worksheet Menu Bar")
.Enabled = True
.Visible = True
End With
With Application.CommandBars("Formatting")
.Enabled = True
.Visible = True
End With
With Application.CommandBars("Standard")
.Enabled = True
.Visible = True
End With
With Application.CommandBars("TranslateIT")
.Enabled = True
.Visible = True
End With
DeleteToolBar
On Error GoTo 0
End Sub