用VBA代碼處理菜單和工具欄之三

字號(hào):

隱藏和顯示菜單和工具欄
    可以用CommandBar對(duì)象的Visible屬性來隱藏和顯示工具欄,當(dāng)你顯示一個(gè)工具欄的時(shí)候,你可以用Position屬性來指定工具欄應(yīng)該顯示在屏幕的哪一個(gè)地方,例如,下面的一段程序需要三個(gè)參數(shù),strCBarName表示要顯示或隱藏的工具欄名稱,blnVisible表示是隱藏還是要顯示,可選參數(shù)lngPosition表示工具欄顯示的位置,默認(rèn)是顯示在access窗口的上方,也可以顯示在左、右或者是下方。
    Function CBToolbarShow(strCBarName As String, _
     blnVisible As Boolean, _
     Optional lngPosition As Long = msoBarTop) As Boolean
     ' This procedure displays or hides the command bar specified in the
     ' strCBarName argument according to the value of the blnVisible
     ' argument. The optional lngPosition argument specifies where the
     ' command bar will appear on the screen.
     Dim cbrCmdBar As CommandBar
     On Error GoTo CBToolbarShow_Err
     Set cbrCmdBar = Application.CommandBars(strCBarName)
     ' Show only toolbars.
     If cbrCmdBar.Type > msoBarTypeNormal Then
     CBToolbarShow = False
     Exit Function
     End If
     ' If Position argument is invalid, set to the default
     ' msoBarTop position.
     If lngPosition < msoBarLeft Or lngPosition > msoBarMenuBar Then
     lngPosition = msoBarTop
     End If
     With cbrCmdBar
     .Visible = blnVisible
     .Position = lngPosition
     End With
     CBToolbarShow = True
    CBToolbarShow_End:
     Exit Function
    CBToolbarShow_Err:
     CBToolbarShow = False
     Resume CBToolbarShow_End
    End Function
    要顯示一個(gè)菜單欄,可以參考以下的一個(gè)函數(shù)來實(shí)現(xiàn):
    Function CBMenuBarShow(strCBarName As String) As Boolean
    ' 本函數(shù)可以顯示一個(gè)指定的菜單欄,如果指定的菜單欄不存在或者是非法的名稱,將返回FALSE
     Dim cbrCBarMenu As CommandBar
     On Error GoTo CBMenuBarShow_Err
     Set cbrCBarMenu = Application.CommandBars(strCBarName)
     If cbrCBarMenu.Type <> msoBarTypeMenuBar Then
     CBMenuBarShow = False
     Exit Function
     End If
     With cbrCBarMenu
     .Visible = True
     End With
     CBMenuBarShow = True
    CBMenuBarShow_End:
     Exit Function
    CBMenuBarShow_Err:
     CBMenuBarShow = False
     Resume CBMenuBarShow_End
    End Function
    調(diào)用方法:? CBMenuBarShow ("Menu Bar")’顯示已被隱藏的主菜單。