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、输入如下的代码
3、需要注意的是:了解XML语言和Office Ribbon定制技术的朋友,每一项元素的值必须要用英文4个双引号引起来。
四、发布该com加载项
单击菜单【文件】→【生成RibbonForWord2007.dll】即可完成加载项的发布。
五、测试
当你打开word2007的时候,如果成功加载首先会提示你“我的com加载项已经成功加载!”,然后会在功能区添加一个选项卡,如图3所示,单击该选项卡的按钮,就会自动输入信息。
六:增加了根据word的版本生成工具栏的判别,如果是word 2007则增加功能区,2003及以前的版本增加菜单。
一、创建外接程序
启动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"" >" & _
"" & _ " & _
"" & _ " & _
"" & _ " & _
"" & _ " & _
"" & _
"
"
"
"
""
GetRibbonXML = sRibbonXML
End Function
'控件回调的过程
Public Sub InsertCompanyName(ByVal control As IRibbonControl)
' Inserts the specified text at the beginning of a range.
Dim MyText As String
Dim MyRange As Object
Set MyRange = oWD.ActiveDocument.Range
MyText = ""http://www.officefans.net/""
' Inserts text at the beginning
' of the active document.
MyRange.InsertBefore (MyText)
End Sub
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 = "" & _ "
"" & _ " & _
"" & _ " & _
"" & _ " & _
"" & _ " & _
"" & _
"
"
"
"
"
GetRibbonXML = sRibbonXML
End Function
'========================================================================
''控件回调的过程
'========================================================================
Public Sub InsertCompanyName(ByVal control As IRibbonControl)
' Inserts the specified text at the beginning of a range.
Dim MyText As String
Dim MyRange As Object
Set MyRange = oWD.ActiveDocument.Range
MyText = "http://www.excelhome.net"
' Inserts text at the beginning
' of the active document.
MyRange.InsertBefore (MyText)
End Sub
'========================================================================
'创建2003菜单
'========================================================================
Private Sub CreateMenu()
Dim NewMenu As Office.CommandBarPopup
Call DeleteMenu
Set NewMenu = oWD.CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup, Temporary:=True)
With NewMenu
.Caption = "sanjie"
Set objButton1 = .Controls.Add(Type:=msoControlButton)
With objButton1
.Caption = "输入信息"
.FaceId = 23
.Tag = "Button1"
End With
Set objButton2 = .Controls.Add(Type:=msoControlButton)
With objButton2
.Caption = "显示当前Word版本"
.FaceId = 116
.Tag = "Button2"
End With
Set objButton3 = .Controls.Add(Type:=msoControlButton)
With objButton3
.Caption = "关于..."
.BeginGroup = True
.FaceId = 984
.Tag = "Button3"
End With
End With
End Sub
'========================================================================
'删除菜单2003
'========================================================================
Private Sub DeleteMenu()
On Error Resume Next
'CommandBars(1).Reset
oWD.CommandBars("Menu Bar").Controls("sanjie").Delete
End Sub
'========================================================================
'按钮1的动作
'========================================================================
Private Sub objButton1_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim MyText As String
Dim MyRange As Object
Set MyRange = oWD.ActiveDocument.Range
MyText = "http://www.excelhome.net"
MyRange.InsertBefore (MyText)
End Sub
'========================================================================
'按钮2的动作
'========================================================================
Private Sub objButton2_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox oWD.Version
End Sub
'========================================================================
'按钮3的动作
'========================================================================
Private Sub objButton3_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox "三戒制作!", vbInformation, Title:="ok"
End Sub
如何用vb6.0编自动输入用户名、密码的程序?
如何用vb6.0编写文件夹加密程序
word的特殊符号,如何用??
如何用VB6.0读取文件的摘要信息?
如何用VB6.0的DATA控件连接ACCESS库?
VB6.0中如何用编程的方法实现打印。。。
如何用Word的“三维功能”?
如何用VB6编写网游登录器?
如何用vb6.0做游戏
请问如何用VB6提取dvd字幕?
如何用WORD看电视
请问如何用word 2000打开word 2003的文档?
如何用GHOST:BIOS里的程序
如何用VFP写出下面的程序:
如何用vb6 的WebBrowser获取单选按钮的值
如何用VB6调用带参数带输出的存储过程?
如何用ListView控件显示指定目录的图片文件(VB6)
如何用VB6开发一套需要上网才能运行的软件
如何用word打印信封
如何用word 打开excel
word 文档如何用公式
如何用word制做小报
如何用WORD打印条形码?
如何用word编辑文件