|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 henrylam 于 2011-11-17 20:37 编辑
网上用VB6.0制作Excel2010功能区COM加载项的教程不少,而用VB6.0自定义图标的资料几乎没有,所以在这方面花了不少时间,以下将这两天的心得整理出来供大家一起学习讨论。Excel2007版的制作过程类似,Excel2003版的不适合。
由于本人水平有限,错漏之处难免,欢迎网友指教。
一、创建外接程序
打开VB6.0,在新建工程对话框中选择“外接程序”。然后在“项目资源管理器”窗口中,通过右键单击“Connect”打开设计器窗口,然后选择“查看对象”。按下图所示进行填写和选择。
在VB6.0菜单栏“工程”点击“引用”,按下图勾选
二、编写代码
在“项目”窗口中,右键单击“Connect”项并选择“查看代码”,删除在设计器代码窗口中自动生成的代码,将以下代码复制进去后保存。
- Option Explicit
- Implements IRibbonExtensibility '添加对 IRibbonExtensibility 接口的引用
- Public xlApp As Excel.Application
- Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
- Set xlApp = Application '加载时
- End Sub
- Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
- Set xlApp = Nothing '卸载时
- End Sub
- '调用自定义 XML
- Public Function IRibbonExtensibility_GetCustomUI(ByVal RibbonID As String) As String
- IRibbonExtensibility_GetCustomUI = GetRibbonXML()
- End Function
- '添加 XML 自定义代码
- Public Function GetRibbonXML() As String
- Dim sRibbonXML As String
- sRibbonXML = "<customUI xmlns=""http://schemas.microsoft.com/office/2006/01/customui"" >" & _
- "<ribbon startFromScratch=""false"">" & _
- "<tabs>" & _
- "<tab id=""henrylam"" label=""我的工具"">" & _
- "<group id=""test"" label=""测试"">" & _
- "<button id=""Button1"" label=""测试内置图标"" size=""large"" imageMso=""HappyFace"" onAction=""test1"" />" & _
- "<button id=""Button2"" label=""测试自定义图标"" size=""large"" getImage=""GetImage"" onAction=""test2"" />" & _
- "</group >" & _
- "<group id=""ExcelHome"" label=""ExcelHome"">" & _
- "<button id=""Button3"" label=""访问论坛"" size=""large"" getImage=""GetImage"" onAction=""test3"" />" & _
- "</group >" & _
- "</tab>" & _
- "</tabs>" & _
- "</ribbon>" & _
- "</customUI>"
- GetRibbonXML = sRibbonXML
- End Function
-
- '下面是调用资源文件(.res)中资源的函数。
- 'Button1用的是系统内置图标,无需调用。Button2和Button3用的是自定义图标
- Function GetImage(control As IRibbonControl) As IPictureDisp
- Select Case control.Id
- Case "Button2"
- Set GetImage = LoadResPicture(101, vbResBitmap) '调用资源文件中的位图作为功能区的图标
- Case "Button3"
- Set GetImage = LoadResPicture(102, vbResBitmap)
- End Select
- End Function
- '下面是测试控件回调的3个过程,对应3个按钮
- Public Sub test1(ByVal control As IRibbonControl)
- MsgBox "测试通过,按钮用的是内置图标", vbInformation
- End Sub
- Public Sub test2(ByVal control As IRibbonControl)
- MsgBox "测试通过,按钮用的是自定义图标", vbInformation
- End Sub
- Public Sub test3(ByVal control As IRibbonControl)
- If Not xlApp.ActiveWorkbook Is Nothing Then
- xlApp.ActiveWorkbook.FollowHyperlink Address:="http://club.excelhome.net/forum.php", NewWindow:=True
- Else
- MsgBox "没有活动工作簿", vbExclamation
- End If
- End Sub
复制代码 |
评分
-
6
查看全部评分
-
|