您的位置: 旅游网 > 明星

vb.net开发中使用excel插件代码

发布时间:2019-09-12 18:33:19

用VB.NET做excel插件开发,可能需要为excel添加自定义的工具栏。这个也是非常的简单的。以下是引用片段:

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

这样程序就完成了,赶紧动手体验吧!

查看本文来源

一岁半宝宝发烧39度怎么办
小儿呕吐吐奶溢乳哭闹怎么办
小孩便秘
脑梗塞有什么症状吗
猜你会喜欢的
猜你会喜欢的