Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制VB.Net版

Posted ryueifu-vba

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制VB.Net版相关的知识,希望对你有一定的参考价值。

Excel-DNA项目中的自定义功能区和自定义任务窗格需要用到各种命名空间、添加所需文件,才能实现。后来我发现可以把所有代码都写在Class1.vb这个默认文件中。

大家可以在Visual Studio中创建一个类库项目(.Net Framework),然后把默认的Class1.vb中的代码整体替换为下面我贴的这个代码。然后启动调试,就可以看到自定义功能区和任务窗格了。

 1 Imports System.Runtime.InteropServices
 2 Imports ExcelDna.Integration
 3 Imports ExcelDna.Integration.CustomUI
 4 Imports Excel = Microsoft.Office.Interop.Excel
 5 <ComVisible(True)>
 6 Public Class Class1
 7     Inherits ExcelRibbon
 8     Implements IExcelAddIn
 9     Public R As IRibbonUI
10     Public Overrides Function GetCustomUI(RibbonID As String) As String
11         Dim xml As XElement
12         xml = <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnLoad">
13                   <ribbon startFromScratch="false">
14                       <tabs>
15                           <tab id="Tab1" label="RibbonXmlEditor">
16                               <group id="Group1" label="Author:ryueifu">
17                                   <button id="Button1" label="CTP" imageMso="C" onAction="Button1_Click"/>
18                                   <button id="Button2" label="UnLoad" imageMso="U" onAction="Button2_Click"/>
19                               </group>
20                           </tab>
21                       </tabs>
22                   </ribbon>
23               </customUI>
24         Return xml.ToString()
25     End Function
26     Public Sub OnLoad(ribbon As IRibbonUI)
27         R = ribbon
28         R.ActivateTab(ControlID:="Tab1")
29     End Sub
30     Public Sub Button1_Click(control As IRibbonControl)
31         ctp.Visible = Not ctp.Visible
32     End Sub
33     Public Sub Button2_Click(control As IRibbonControl)
34         Dim ThisAddin As Excel.AddIn
35         ThisAddin = ExcelDnaUtil.Application.AddIns.Item(Index:=My.Application.Info.AssemblyName)
36         ThisAddin.Installed = False
37     End Sub
38 
39     Public Sub AutoOpen() Implements IExcelAddIn.AutoOpen
40         Module1.CreateCTP()
41     End Sub
42 
43     Public Sub AutoClose() Implements IExcelAddIn.AutoClose
44         Module1.DisposeCTP()
45     End Sub
46 End Class
47 
48 Public Module Module1
49     Public uc As System.Windows.Forms.UserControl
50     Public ctp As CustomTaskPane
51     Public Sub CreateCTP()
52         uc = New Windows.Forms.UserControl
53         ctp = CustomTaskPaneFactory.CreateCustomTaskPane(userControl:=uc, title:="CTP")
54         With ctp
55             .DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight
56             .Visible = True
57         End With
58     End Sub
59     Public Sub DisposeCTP()
60         ctp.Dispose()
61     End Sub
62 End Module

 

以上是关于Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制VB.Net版的主要内容,如果未能解决你的问题,请参考以下文章

VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制C#版

VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制VB.Net版

10分钟零基础带你入门Ribbon小项目-啥?小白都能看懂?

tomcat下部署多个项目,配置文件中的ribbon超时不生效是啥原因?

Java之 Spring Cloud 微服务搭建Ribbon(第一个阶段)SpringBoot项目实现商品服务器端是调用

SpringCloud负载均衡-Ribbon