win7服务无法打开:如何用VB6的COM 外接程序对word2007功能区进行定制(4.24日更新代码)

来源:百度文库 编辑:中财网 时间:2024/04/28 16:07:44
用VB6的COM 外接程序可以方便的对office系列软件进行定制,下面是我在制作过程中的体会。
一、创建外接程序
启动VB,在新建工程对话框中选择“外接程序” 二、初始定制
1、在“项目资源管理器”窗口中,通过右键单击“Connect”打开设计器窗口,然后选择“查看对象”。从“应用程序”列表中选择“Microsoft Word”。
2、在“初始加载行为”列表中,选择“Startup”。
3、将外接程序显示名称更改为“Word 2007功能区测试 ”。4、在项目资源管理器中,右键单击“MyAddin”,然后在“属性”窗口中将该外接程序的名称更改为“RibbonForWord2007”。
5、从该项目中删除“frmAddin”(这个在本例中暂时不需要)。
6、在工程-引用中将Microsoft Office Object 8.0更改为Microsoft Office Object 12。(备注:只有这样才能定制和添加对 IRibbonExtensibility 接口的引用,方法,先去掉Microsoft Office Object 8.0,再重新进入工程->引用后会有Microsoft Office Object 12.0选项
三、输入代码
1、在“项目”窗口中,右键单击“Connect”项并选择“查看代码”。在设计器的代码窗口中删除所有代码,因为这些代码可用于 Visual Basic 外接程序而不适用于 Microsoft Office 外接程序。
2、输入如下的代码

Option Explicit
Dim oWD As Object
Implements IRibbonExtensibility2 '添加对 IRibbonExtensibility 接口的引用

'启动
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Set oWD = Application
MsgBox "我的com加载项已经成功加载!"
End Sub

'实现IRibbonExtensibility接口的唯一成员 GetCustomUI,此过程调用 GetRibbonXML 方法,正如其名称所示,
'该方法将自定义 XML 返回到 GetCustomUI 方法,后者然后将自定义 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

    sRibb"http://schemas.microsoft.com/office/2006/01/customui"" >" & _
                "" & _
                "" & _
                "" & _
                "" & _
                "

3、需要注意的是:了解XML语言和Office Ribbon定制技术的朋友,每一项元素的值必须要用英文4个双引号引起来。
四、发布该com加载项
单击菜单【文件】→【生成RibbonForWord2007.dll】即可完成加载项的发布。
五、测试
当你打开word2007的时候,如果成功加载首先会提示你“我的com加载项已经成功加载!”,然后会在功能区添加一个选项卡,如图3所示,单击该选项卡的按钮,就会自动输入信息。
六:增加了根据word的版本生成工具栏的判别,如果是word 2007则增加功能区,2003及以前的版本增加菜单。
Option Explicit

Private oWD As Word.Application
Private WithEvents objButton1 As Office.CommandBarButton
Private WithEvents objButton2 As Office.CommandBarButton
Private WithEvents objButton3 As Office.CommandBarButton

Implements IRibbonExtensibility '添加对 IRibbonExtensibility 接口的引用

'========================================================================
  ''启动时执行的任务
'========================================================================

Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Set oWD = Application
Select Case oWD.Version
      Case "12.0"
        MsgBox "你正使用的版本是Office 12"
      Case "11.0"
        MsgBox "你正使用的版本是Office 11"
        Call CreateMenu
      Case Else
        MsgBox oWD.Version
        Call CreateMenu
End Select
        
End Sub


'========================================================================
  '移除时执行的任务
'========================================================================
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
  Call DeleteMenu
  Set objButton1 = Nothing
  Set objButton2 = Nothing
  Set objButton3 = Nothing
  Set oWD = Nothing
End Sub


'========================================================================
  ''实现IRibbonExtensibility接口的唯一成员 GetCustomUI,此过程调用 GetRibbonXML 方法,正如其名称所示,
'该方法将自定义 XML 返回到 GetCustomUI 方法,后者然后将自定义 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 = "" & _
                "" & _
                "" & _
                "" & _
                "" & _
                "