VB.Net开发Excel COM AddIn
上一篇 / 下一篇 2008-06-01 18:52:03 / 个人分类:VB技术难点
今天把以往在Excel中用VBA写的的一些实例翻出来了看了看,大部分都是些Excel下的AddIn文件即加载宏(.xla)。突然间很想把这些实例转换为COM AddIn文件。在VB6下开发Excel的COM AddIn感觉比较复杂,看了些相关方面的书,但是一直没有着手自己编写一个实例出来。反正也没有更高的需求,也就没有在深入探讨这方面的东西了。今天闲来无事,就试试用VB.Net做一下开发试试。试过之后,感觉要比VB6方便多了。这里给出的示例是,实现工作簿中隐藏与取消隐藏工作表。
(1)首先开启VB.Net然后新建一个项目。依次选择“其他项目类型”--“扩展性”--“共享的外接程序”。修改好名称、位置后,选择确定按钮。
(2)在随后的连续设置对话框中1号对话框选择“使用VB创建外接程序”。二号对话框选择“Microsoft Excel”。三号对话框中设置外接程序名称和说明。四号对话框勾选两个复选框。五号对话框选择完成按钮。
(3)在资源管理器窗口中右击工程,然后选择属性。选择引用栏,查看是否引用了以下内容。如果没有选择添加按钮,然后依次将这些引用添加进来。
Extensibility(V7.0.3300.0)
Microsoft Excel 12.0 Object Library(ActiveX V1.6.0.0)
Microsoft Office 12.0 Object Library(ActiveX V2.4.0.0)
Microsoft Visual Basic for Applications Extensibility 5.3(ActiveX V5.3.0.0)
System
System.Data
System.Drawing
System.Windows.Forms
System.Xml
(4)在资源管理器窗口中找到connect.vb并打开其代码窗口。然后在该其中输入以下代码
Imports Extensibility精英博客.qE
zuY#[2B6H
Imports System.Runtime.InteropServices精英博客7g;e`K:VIH1x-l
Imports Microsoft.Office.Core
yN(V^#L5i;h0Imports Microsoft.Office.Core.MsoControlType
精英博客e)xl1RSNI
#Region " Read me for Add-in installation and setup information. "精英博客S7AO.}U$Ekx
' When run, the Add-in wizard prepared the registry for the Add-in.
sOG;ccu$C+o+X0' At a later time, if the Add-in becomes unavailable for reasons such as:
&v:g2n9EB{D0vp0' 1) You moved this project to a computer other than which is was originally created on.
XHnF_?MgW0' 2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.精英博客e&aJ
Xa.T)D
' 3) Registry corruption.
pf
V@2ZN [;|0' you will need to re-register the Add-in by building the $SAFEOBJNAME$Setup project, 精英博客 x5Wh^h
' right click the project in the Solution Explorer, then choose install.精英博客:T*FI#dy&F
#End Region
<GuidAttribute("4C1CFBC7-551D-4B81-B282-7E4A7D9D71DD"), ProgIdAttribute("MyCOMAddin.Connect")> _
n;MkFl2Qf!Xt0Public Class Connect
1jj7|w
^7~DN0
0wKt:]&]
|.e9}3`0 Implements Extensibility.IDTExtensibility2
b3@@"MSx0 Dim WithEvents MyButton As CommandBarButton
.e/{O7mg0 Dim applicationObject As Object
(AS*TvnK&o0 Dim addInInstance As Object精英博客{#M\J#o9yU(vd
^BIMD
^P0r0 Public Sub OnBeginShutdown(ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnBeginShutdown精英博客LKl]H.w%z
On Error Resume Next
Vu-`}]H)TbX2PY}6D0 MyButton.Delete()精英博客9H$T.s9YnkI/Z
C
MyButton = Nothing