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 Sub 11 Protected Overrides Function CreateRibbonExtensibilityObject() As IRibbonExtensibility 12 Return New Ribbon1() 13 End Function 14 End Class 15 16 <System.Runtime.InteropServices.ComVisible(True)> 17 Public Class Ribbon1 18 Implements IRibbonExtensibility 19 Public R As IRibbonUI 20 Public Function GetCustomUI(RibbonID As String) As String Implements IRibbonExtensibility.GetCustomUI 21 Dim xml As XElement 22 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 Function 36 Public Sub OnLoad(ribbon As IRibbonUI) 37 R = ribbon 38 R.ActivateTab(ControlID:="Tab1") 39 End Sub 40 Public Sub Button1_Click(control As IRibbonControl) 41 ctp.Visible = Not ctp.Visible 42 End Sub 43 Public Sub Button2_Click(control As IRibbonControl) 44 Dim ThisCOM As COMAddIn 45 ThisCOM = Globals.ThisAddIn.Application.COMAddIns.Item(Index:=My.Application.Info.AssemblyName) 46 ThisCOM.Connect = False 47 End Sub 48 End Class 49 50 Public Module Module1 51 Public uc As System.Windows.Forms.UserControl 52 Public ctp As Microsoft.Office.Tools.CustomTaskPane 53 Public Sub CreateCTP() 54 uc = New Windows.Forms.UserControl 55 ctp = Globals.ThisAddIn.CustomTaskPanes.Add(control:=uc, title:="CTP") 56 With ctp 57 .DockPosition = MsoCTPDockPosition.msoCTPDockPositionRight 58 .Visible = True 59 End With 60 End Sub 61 Public Sub DisposeCTP() 62 ctp.Dispose() 63 End Sub 64 End Module
原文地址:https://www.cnblogs.com/ryueifu-VBA/p/10122590.html
时间: 2024-10-03 00:36:32