做一个带WPF窗体的Access插件-1

一、关于起因

最近很多的一些小项目使用Access做数据库,其中碰到些很恼火的问题就是给表字段设置默认值。每个数据库的表都比较多并且每个表需要设置默认值的字段又比较多。如果人工去打开表一个个字段去设置确实是吃力不讨好。所以想把默认值放在一个表里面,通过一个插件来操作,完成批量设置。说动手就动手,开发过程中,碰到了好几个问题。

二、问题一

如果用VSTO来做,会发现VSTO里根本没有Access的模板。所以选择使用共享插件模板来开发。那想到VS从2012开始已经没有共享插件模板,所以只好再安装了VS2010来做,共享插件模板再VS2010的位置如下所示。

三、问题二

既然是Access,属于Office下的一个套件,至少得用一下Ribbon吧,结果共享插件里不能像VSTO模板里一样,直接添加带设计视图的Ribbon菜单文件。最后只好采取自己手工添加RibbonXML的方法。

以下是插件的类模块Connect.vb中的代码。其中的Access应用程序对象在插件退出时,做了立即销毁处理,如果不这样处理,可能会造成Access程序退出时,还存在Access应用程序Com对象的引用存在。

 1 Imports Extensibility
 2 Imports System.Runtime.InteropServices
 3 Imports Microsoft.Office.Interop
 4 Imports Microsoft.Office.Core
 5 
 6 #Region " Read me for Add-in installation and setup information. "
 7 ' When run, the Add-in wizard prepared the registry for the Add-in.
 8 ' At a later time, if the Add-in becomes unavailable for reasons such as:
 9 '   1) You moved this project to a computer other than which is was originally created on.
10 '   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.
11 '   3) Registry corruption.
12 ' you will need to re-register the Add-in by building the AccessHelperSetup project, 
13 ' right click the project in the Solution Explorer, then choose install.
14 #End Region
15 
16 <GuidAttribute("CFEFF0B2-8C51-432B-A7B2-478C732A7C64"), ProgIdAttribute("AccessHelper.Connect")> _
17 Public Class Connect
18 
19     Implements Extensibility.IDTExtensibility2
20     Implements IRibbonExtensibility
21 
22     Private mAccApp As Access.Application
23     Private mAddInInstance As Object
24 
25     Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown
26 
27     End Sub
28 
29     Public Sub OnAddInsUpdate(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
30     End Sub
31 
32     Public Sub OnStartupComplete(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnStartupComplete
33     End Sub
34 
35     Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
36         mAccApp = Nothing
37         mAddInInstance = Nothing
38         GC.Collect()
39         GC.WaitForPendingFinalizers()
40     End Sub
41 
42     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
43         mAccApp = CType(application, Access.Application)
44         mAddInInstance = addInInst
45     End Sub
46 
47     Public Function GetCustomUI(ByVal RibbonID As String) As String Implements Microsoft.Office.Core.IRibbonExtensibility.GetCustomUI
48         Return My.Resources.RibbonMenuXML
49     End Function
50 
51     ''' <summary>
52     ''' 统一处理所有Ribbon菜单控件的OnAction属性
53     ''' </summary>
54     ''' <param name="control">当前需要回调处理的Ribbon控件</param>
55     ''' <remarks></remarks>
56     Public Sub RibbonAction(ByVal control As IRibbonControl)
57         Select Case control.Id
58             Case Is = "mbtnTableEditor"
59                 'Dim frmTableEditor As New FTableEditor(mAccApp)
60                 'frmTableEditor.Show()
61                 Dim wTblEditor As New WTableEditor(mAccApp)
62                 wTblEditor.Show()
63         End Select
64     End Sub
65 
66 End Class
四、问题三

想在插件中使用WPF窗体,而非传统的WinForm。结果发现在为共享插件项目添加新项时,根本没法找到WPF窗体这个项目。只好自己手工建一个VS的项模板,创建自定义的项模板步骤如下所示。

原文地址:https://www.cnblogs.com/alexywt/p/6801807.html