Microsoft Access的COM加载项可以响应正在打开或关闭的数据库文件吗?

时间:2019-05-16 22:32:09

标签: vb.net visual-studio vsto office-addins

Microsoft Access 2007的VB.Net COM加载项可以响应打开或关闭数据库文件吗?那以后的版本呢?。

我想创建一个执行各种功能的COM加载项。我是这个主题的新手,在经过大量的搜索和阅读之后,我能够整理下面显示的代码。

我希望它对以下三个事件做出回应:

  1. 访问已打开至其“起始页”(未打开文件)。
  2. 一个数据库文件正在打开。
  3. 数据库文件正在关闭。

这是在Access 2007的Visual Studio 2008中创建的。我最初使用的是“可扩展性共享外接程序”向导,但没有用。我在网上找到了C#的示例,并能够将其转换为VB并使其正常运行。这还处于起步阶段,仅向功能区添加了一个按钮。

我仍然不清楚VSTO加载项与“ COM使用者”加载项之间的区别,因此我不知道有什么可用。我知道Access本身没有这些事件。但是,即使没有可靠的响应方式,我也可以使用一种轮询技术,使该附件每秒对Access进行一次ping操作,以查看文件是否打开。

Imports Extensibility
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports Microsoft.Office.Core

<GuidAttribute("04AE57EF-2D3A-496A-8A4E-43930FC9CF83"), _
 ProgIdAttribute("AccessDatabaseExplorer.Connect")> _
Public Class Connect

    Implements IDTExtensibility2

    Private app As Object
    Private addin As Object
    Private WithEvents RibbonButton1 As CommandBarButton

    Public Sub OnConnection( _
                    ByVal application As Object, _
                    ByVal connectMode As Extensibility.ext_ConnectMode, _
                    ByVal addInInst As Object, _
                    ByRef custom As System.Array) _
                    Implements Extensibility.IDTExtensibility2.OnConnection
        app = application
        addin = addInInst
        If connectMode <> ext_ConnectMode.ext_cm_Startup Then
            OnStartupComplete(custom)
        End If
    End Sub

    Public Sub OnStartupComplete( _
                    ByRef custom As System.Array) _
                    Implements Extensibility.IDTExtensibility2.OnStartupComplete
        CreateRibbonButtons()
    End Sub

    Public Sub OnBeginShutdown( _
                    ByRef custom As System.Array) _
                    Implements Extensibility.IDTExtensibility2.OnBeginShutdown
        Dim omissing As Object = System.Reflection.Missing.Value
        RibbonButton1.Delete(omissing)
        RibbonButton1 = Nothing
    End Sub

    Public Sub OnDisconnection( _
                    ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
                    ByRef custom As System.Array) _
                    Implements Extensibility.IDTExtensibility2.OnDisconnection
        If RemoveMode <> ext_DisconnectMode.ext_dm_HostShutdown Then
            OnBeginShutdown(custom)
        End If
        app = Nothing
    End Sub

    Public Sub OnAddInsUpdate( _
                    ByRef custom As System.Array) _
                    Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
        'Do nothing.
    End Sub

    Private Sub CreateRibbonButtons()

        Dim oCommandBars As CommandBars
        Dim oStandardBar As CommandBar

        oCommandBars = DirectCast(app.GetType().InvokeMember("CommandBars", _
                       BindingFlags.GetProperty, Nothing, app, Nothing), CommandBars)

        oStandardBar = oCommandBars("Database")

        'In case the button was not deleted, use the exiting one.
        Try
            RibbonButton1 = DirectCast(oStandardBar.Controls("My Custom Button"), _
                            CommandBarButton)
        Catch ex As Exception
            Dim omissing As Object = CObj(System.Reflection.Missing.Value)
            RibbonButton1 = DirectCast(oStandardBar.Controls.Add(1, omissing, _
                            omissing, omissing, omissing), CommandBarButton)
            RibbonButton1.Caption = "My Custom Button"
            RibbonButton1.Style = MsoButtonStyle.msoButtonIconAndCaptionBelow
        End Try

        RibbonButton1.Tag = "My Custom Button"
        RibbonButton1.OnAction = "!<MyCOMAddin.Connect>"
        RibbonButton1.Visible = True

        Dim oName As Object = CObj(app.GetType().InvokeMember("Name", 
                              BindingFlags.GetProperty, Nothing, app, Nothing))

        oStandardBar = Nothing
        oCommandBars = Nothing

    End Sub

    Private Sub RibbonButton1_Click( _
                    ByVal Ctrl As CommandBarButton, _
                    ByRef CancelDefault As Boolean) _
                    Handles RibbonButton1.Click
        MessageBox.Show("RibbonButton1 was Clicked", "MyCOMAddin")
    End Sub

End Class

0 个答案:

没有答案