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

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

大家可以在Visual Studio中创建一个外接程序项目,然后把ThisAddin.vb中的代码整体替换为下面我贴的这个代码。然后启动调试,就可以看到自定义功能区和任务窗格了。

1 Imports Microsoft.Office.Core 2 Public Class ThisAddIn 3  4     Private Sub ThisAddIn_Startup() Handles Me.Startup 5         CreateCTP() 6     End Sub 7  8     Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 9         DisposeCTP()10     End Sub11     Protected Overrides Function CreateRibbonExtensibilityObject() As IRibbonExtensibility12         Return New Ribbon1()13     End Function14 End Class15 16 <System.Runtime.InteropServices.ComVisible(True)>17 Public Class Ribbon118     Implements IRibbonExtensibility19     Public R As IRibbonUI20     Public Function GetCustomUI(RibbonID As String) As String Implements IRibbonExtensibility.GetCustomUI21         Dim xml As XElement22         xml = <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnLoad">23                   <ribbon startFromScratch="false">24                       <tabs>25                           <tab id="Tab1" label="RibbonXmlEditor">26                               <group id="Group1" label="Author:ryueifu">27                                   <button id="Button1" label="CTP" imageMso="C" onAction="Button1_Click"/>28                                   <button id="Button2" label="UnLoad" imageMso="U" onAction="Button2_Click"/>29                               </group>30                           </tab>31                       </tabs>32                   </ribbon>33               </customUI>34         Return xml.ToString()35     End Function36     Public Sub OnLoad(ribbon As IRibbonUI)37         R = ribbon38         R.ActivateTab(ControlID:="Tab1")39     End Sub40     Public Sub Button1_Click(control As IRibbonControl)41         ctp.Visible = Not ctp.Visible42     End Sub43     Public Sub Button2_Click(control As IRibbonControl)44         Dim ThisCOM As COMAddIn45         ThisCOM = Globals.ThisAddIn.Application.COMAddIns.Item(Index:=My.Application.Info.AssemblyName)46         ThisCOM.Connect = False47     End Sub48 End Class49 50 Public Module Module151     Public uc As System.Windows.Forms.UserControl52     Public ctp As Microsoft.Office.Tools.CustomTaskPane53     Public Sub CreateCTP()54         uc = New Windows.Forms.UserControl55         ctp = Globals.ThisAddIn.CustomTaskPanes.Add(control:=uc, title:="CTP")56         With ctp57             .DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight58             .Visible = True59         End With60     End Sub61     Public Sub DisposeCTP()62         ctp.Dispose()63     End Sub64 End Module
(0)

相关推荐