Excel-DNA項(xiàng)目中的自定義功能區(qū)和自定義任務(wù)窗格需要用到各種命名空間、添加所需文件,才能實(shí)現(xiàn),。后來我發(fā)現(xiàn)可以把所有代碼都寫在Class1.vb這個默認(rèn)文件中,。 大家可以在Visual Studio中創(chuàng)建一個類庫項(xiàng)目(.Net Framework),,然后把默認(rèn)的Class1.vb中的代碼整體替換為下面我貼的這個代碼,。然后啟動調(diào)試,,就可以看到自定義功能區(qū)和任務(wù)窗格了,。 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
|
|