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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。