【发布时间】:2016-10-04 15:09:12
【问题描述】:
我已经加入了这些方法来创建一个宏以分配给右键单击菜单按钮。目标是通过右键单击选择光标下的单词,单击宏,选择该单词(修剪空格)并将其发送到 Word 的默认搜索对话框。
Option Explicit
Sub CreateMacro()
Dim MenuButton As CommandBarButton
With CommandBars("Text")
Set MenuButton = .Controls.Add(msoControlButton)
With MenuButton
.Caption = "Find word"
.Style = msoButtonCaption
.OnAction = "FindWordUnderCursor"
End With
End With
End Sub
Sub ResetRightClick()
Application.CommandBars("Text").Reset
End Sub
Sub FindWordUnderCursor()
Dim pos As Long
Dim myRange As Range
'~~> if the cursor is at the end of the word
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Do While Len(Trim(Selection.Text)) = 0
'~~> Move one character behind so that the cursor is
'~~> at the begining or in the middle
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
'~~> Expand to get the word
Selection.Expand Unit:=wdWord
If Selection.Characters(Selection.Characters.Count) = " " Then
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
End If
'~~> Display the word
Debug.Print Selection.Text
End Sub
唯一需要实现的是所选单词将自身置于默认的 MS-Word 搜索对话框中(或者如果它不活动,则打开它),它会自动突出显示文档中出现的所有内容。当然,如果我右键单击另一个单词并选择宏,则新单词必须替换前一个单词。可以帮忙吗?
以下是两个所需的步骤:
谢谢
【问题讨论】: