如何在ms访问中创建快捷菜单?

时间:2016-03-08 18:23:54

标签: ms-access

如何在ms访问中创建快捷菜单?通过快捷菜单,我的意思是当用户右键单击并显示菜单时。

下面是我编写的用于创建此快捷菜单但收到错误的代码。

编译错误:未定义用户定义的类型

在线:Dim cmbRightClick As Office.CommandBar

Option Compare Database
Option Explicit

Sub CreateReportShortcutMenu()
    Dim cmbRightClick As Office.CommandBar
    Dim cmbControl As Office.CommandBarControl

   ' Create the shortcut menu.
    Set cmbRightClick = CommandBars.Add("cmdReportRightClick", msoBarPopup, False, True)

    With cmbRightClick

        ' Add the Print command.
        Set cmbControl = .Controls.Add(msoControlButton, 2521, , , True)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Quick Print"

        ' Add the Print command.
        Set cmbControl = .Controls.Add(msoControlButton, 15948, , , True)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Select Pages"

        ' Add the Page Setup... command.
        Set cmbControl = .Controls.Add(msoControlButton, 247, , , True)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Page Setup"

        ' Add the Mail Recipient (as Attachment)... command.
        Set cmbControl = .Controls.Add(msoControlButton, 2188, , , True)
        ' Start a new group.
        cmbControl.BeginGroup = True
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Email Report as an Attachment"

        ' Add the PDF or XPS command.
        Set cmbControl = .Controls.Add(msoControlButton, 12499, , , True)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Save as PDF/XPS"

        ' Add the Close command.
        Set cmbControl = .Controls.Add(msoControlButton, 923, , , True)
        ' Start a new group.
        cmbControl.BeginGroup = True
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Close Report"
    End With

    Set cmbControl = Nothing
    Set cmbRightClick = Nothing
End Sub

Private Sub Report_Load()
    CreateReportShortcutMenu
End Sub

2 个答案:

答案 0 :(得分:1)

在VBA编辑器中,打开工具 - >参考,并检查参考 Microsoft Office xx.0对象库(xx是您的Office版本,例如14 for Office 2010)。

使用此引用,Access将识别Office.CommandBar,编译错误将消失。

:您需要添加一些内容才能使其发挥作用。

您正在创建的命令栏"cmdReportRightClick"将永久存储在数据库中。您需要在结束报告时将其删除:

CommandBars("cmdReportRightClick").Delete

或在创建之前检查它的存在。

您需要将其分配给报告:

Private Sub Report_Load()
    CreateReportShortcutMenu
    Me.ShortcutMenuBar = "cmdReportRightClick"
End Sub

答案 1 :(得分:-1)

对于那些 Office 365 桌面版用户,请注意引用的名称可能是 Office 而不是 Microsoft Office 16.0 Object Library。如果没有此引用,Access 将无法识别 Office.CommandBar

这是与 Microsoft Office 16.0 Access database engine object 不同的引用。 这花了我一段时间才弄明白。

enter image description here