`
wms20070910
  • 浏览: 95527 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

WORD VBA 右键菜单添加Google或Baidu搜索功能

阅读更多
Private Sub Document_Close()
    On Error Resume Next
    Application.CommandBars("Text").Controls("Google搜索").Delete   '恢复原有菜单
    Application.CommandBars("Text").Controls("Baidu搜索").Delete   '恢复原有菜单
    Application.CommandBars("Text").Reset '重新设置右键菜单,彻底恢复默认设置
End Sub

'----------------------
Private Sub Document_Open()
    On Error Resume Next
    Dim BtnGoogle As CommandBarButton
    Dim BtnBaidu As CommandBarButton
    Application.CommandBars("Text").Controls("Google搜索").Delete   '预防性删除
    Application.CommandBars("Text").Controls("Baidu搜索").Delete   '预防性删除
    Application.CommandBars("Text").Reset '重新设置右键菜单,彻底恢复默认设置
    Set BtnGoogle = Application.CommandBars("Text").Controls.Add(Type:=msoControlButton, Before:=1) '第一项
    Set BtnBaidu = Application.CommandBars("Text").Controls.Add(Type:=msoControlButton, Before:=2) '第二项
    With BtnGoogle
      .Caption = "&Google搜索" '命令名称
      .FaceId = 86             '命令的FaceId,字母G
      .Visible = True           '可见
      .OnAction = "GoogleSearch"       '指定响应过程名
    End With
    With BtnBaidu
      .Caption = "&Baidu搜索" '命令名称
      .FaceId = 81             '命令的FaceId
      .Visible = True           '可见
      .OnAction = "BaiduSearch"       '指定响应过程名
    End With
End Sub

'----------------------
Sub GoogleSearch()
    Dim sSearch$, sSel$
    sSel = Trim(Selection.Text)
    If Len(sSel) <= 1 Then
      MsgBox "选择的文本太少,不能进行搜索!" & vbCrLf & "最少两个字符!!! ", vbInformation + vbOKOnly, "Google搜索"
    Else
      sSearch = "explorer ""http://www.google.cn/search?client=Juncox&hl=zh-CN&q=" & sSel & """"    '两种形式结果是一致的
      sSearch = "explorer " & Chr(34) & "http://www.google.cn/search?client=Juncox&hl=zh-CN&q=" & sSel & Chr(34)
      Shell sSearch
    End If
End Sub

Sub BaiduSearch()
    Dim sSearch$, sSel$
    sSel = Trim(Selection.Text)
    If Len(sSel) <= 1 Then
      MsgBox "选择的文本太少,不能进行搜索!" & vbCrLf & "最少两个字符!!! ", vbInformation + vbOKOnly, "Google搜索"
    Else
      sSearch = "explorer ""http://www.baidu.com/baidu?tn=Juncox&word=" & sSel & """"
      sSearch = "explorer " & Chr(34) & "http://www.baidu.com/baidu?tn=Juncox&word=" & sSel & Chr(34)
      Shell sSearch
    End If
End Sub
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics