1。新建>外接程序(需要VB企业版)
2。然后双设计器下的Connect,在出来的对话框中,将“应用程序”选“Microsoft Word”。
3。在工程>引用中添加对“Microsoft Word xx.0 Object Library”的引用。
4。在connect上点右键,选查看代码
5。编写代码,下面的示例代码演示如何向word添加一个QuickSearch工具栏,并在上面添加一个“划词搜索”按钮,在word中选中一些文字后点击此按钮,会打开一个IE窗口在百度中搜索当前选中的文本:
Option Explicit
Private WithEvents wdApp As Word.Application
Dim myBar As CommandBar
Private WithEvents myButton As Office.CommandBarButton
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Set wdApp = Application
On Error Resume Next
wdApp.CommandBars("QuickSearch").Delete
On Error GoTo 0
Set myBar = wdApp.CommandBars.Add("QuickSearch")
myBar.Visible = True
Set myButton = myBar.Controls.Add(msoControlButton)
With myButton
.Style = msoButtonCaption
.Caption = "WORD划词搜索"
End With
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
wdApp.CommandBars("QuickSearch").Delete
On Error GoTo 0
Set wdApp = Nothing
End Sub
Private Sub myButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
Dim ie
If wdApp.Selection.Text <> "" Then
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate "http://www.baidu.com/s?wd=" & wdApp.Selection
End If
End Sub
Private Sub wdApp_WindowSelectionChange(ByVal Sel As Word.Selection)
If Sel.Text = "" Then
myButton.Enabled = False
Else
myButton.Enabled = True
End If
End Sub
6.编译为Dll后再打开word就可以看到“QuickSearch”工具栏了,如果要复制到其他电脑上使用,则需要使用“regsvr32 [所编译的dll文件的完整路径]”命令先注册一下,或者使用“工具>COM加载宏”菜单命令加载或卸载。
使用VB6.0给word做插件可以实现的不仅仅是上述这些,还可以修改word程序的右键菜单,调用word中众多对象的各种属性、方法,响应word提供的事件等,同时兼有VB6中可以使用自定义控件、控件数组、资源文件、代码不易被破解等优点来弥补word VBA的不足。