PublicClassConnectClassConnect ImplementsExtensibility.IDTExtensibility2 DimappAsExcel.Application DimwbAsExcel.Workbook DimwsAsExcel.Worksheet DimaddInInstanceAsObject DimpasteTextAsMicrosoft.Office.Core.CommandBarButton‘定义一个工具栏按钮 PublicSubOnBeginShutdown()SubOnBeginShutdown(ByRefcustomAsSystem.Array)ImplementsExtensibility.IDTExtensibility2.OnBeginShutdown EndSub PublicSubOnAddInsUpdate()SubOnAddInsUpdate(ByRefcustomAsSystem.Array)ImplementsExtensibility.IDTExtensibility2.OnAddInsUpdate EndSub PublicSubOnStartupComplete()SubOnStartupComplete(ByRefcustomAsSystem.Array)ImplementsExtensibility.IDTExtensibility2.OnStartupComplete EndSub PublicSubOnDisconnection()SubOnDisconnection(ByValRemoveModeAsExtensibility.ext_DisconnectMode,ByRefcustomAsSystem.Array) ImplementsExtensibility.IDTExtensibility2.OnDisconnection EndSub PublicSubOnConnection()SubOnConnection(ByValapplicationAsObject,ByValconnectModeAsExtensibility.ext_ConnectMode, ByValaddInInstAsObject,ByRefcustomAsSystem.Array) ImplementsExtensibility.IDTExtensibility2.OnConnection IfTypeOf(application)IsExcel.ApplicationThenapp=CType(application,Excel.Application) addInInstance=addInInst wb=app.ActiveWorkbook ws=CType(wb.ActiveSheet,Excel.Worksheet) DimtoolbarAsMicrosoft.Office.Core.CommandBar=Nothing IfNotappIsNothingThen toolbar=AddToolbar(app,"专用工具栏")’添加工具栏 EndIf ’建立按钮添加文本 pasteText=MakeANewButton(toolbar,"Inserttext", 1044,AddressOfpasteText_Click)’添加工具栏按钮并帮定事件 EndSub ’添加工具栏 PrivateFunctionAddToolbar()FunctionAddToolbar(ByValexAsExcel.Application,_ ByValtoolbarNameAsString)AsMicrosoft.Office.Core.CommandBar DimtoolBarAsMicrosoft.Office.Core.CommandBar=Nothing Try ’为add-in建立一个命令条 toolBar=CType(ex.CommandBars.Add(toolbarName, Microsoft.Office.Core.MsoBarPosition.msoBarTop,True), Microsoft.Office.Core.CommandBar) toolBar.Visible=True ReturntoolBar Catch ReturnNothing EndTry EndFunction ’在工具栏上添加一个按钮 PrivateFunctionMakeANewButton()FunctionMakeANewButton(ByValcommandBarAsMicrosoft.Office.Core.CommandBar,ByValcaption AsString,ByValfaceIDAsInteger,ByValclickHandlerAsMicrosoft.Office. Core._CommandBarButtonEvents_ClickEventHandler)AsMicrosoft.Office.Core.CommandBarButton Try DimnewButtonAsMicrosoft.Office.Core.CommandBarButton newButton=CType(commandBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton),Microsoft.Office.Core.CommandBarButton) newButton.Caption=caption newButton.FaceId=faceID AddHandlernewButton.Click,clickHandler ReturnnewButton CatchexAsSystem.Exception ReturnNothing EndTry EndFunction ’点击工具条按钮时应该执行的程序 PublicSubpasteText_Click()SubpasteText_Click(ByValbarButtonAsMicrosoft.Office.Core.CommandBarButton,ByRefsomeBoolAsBoolean) DimtextAsString="" DimdataAsSystem.Windows.Forms.IDataObject=System.Windows.Forms.Clipboard.GetDataObject() Ifdata.GetDataPresent(System.Windows.Forms.DataFormats.Text)Then text=data.GetData(System.Windows.Forms.DataFormats.Text).ToString() If(NotappIsNothing)Then Me.app.ActiveCell.Value=text EndIf EndIf EndSub EndClass
原文链接:https://www.f2er.com/vb/262203.html