如果输入了某个文本,然后按下了F9,我想运行一个宏,例如:
The patient has been diagnosed with DIA1 ,然后是F9键。
Word应对F9键作出反应,并运行宏DIA1,该宏将从键入的文本中删除单词"DIA1“并插入特定的文本。
我怎么能这么做?
发布于 2017-07-26 00:40:07
两件事:
1-解决方案在最后一行中找到"DIA1“,它不检查最后一个单词。如果你坚持这里的最后一句话,那就是:
Sub Lastword()
Dim rng As Range, wrd As String
Set rng = ActiveDocument.Paragraphs.Last.Range
wrd = StrReverse(Trim(Left(StrReverse(rng), InStr(1, StrReverse(rng), " ", vbTextCompare))))
MsgBox wrd
End Sub2-F9-F12被保留,所以您最近的选项是F8。或者,您可以按照@Ibo的建议,将宏分配给F9,方法是遵循这些步骤。
解决方案:
1-将F8绑定到宏
Sub Bind()
Application.CustomizationContext = ThisDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF8), KeyCategory:=wdKeyCategoryMacro, Command:="Runme"
End Sub2-“查找和替换”宏:
Sub Runme()
Dim rng As Range
Set rng = ActiveDocument.Paragraphs.Last.Range
rng.Find.Execute FindText:="DIA1", ReplaceWith:="hello", Replace:=wdReplaceAll
End Sub3-如果需要解除F8的绑定:
Sub Unbind()
CustomizationContext = NormalTemplate
FindKey(BuildKeyCode(wdKeyF8)).Clear
End Sub发布于 2017-07-25 23:51:27
您需要定义在F9上触发的宏,然后将F9分配给它。学习如何
假设这个宏的名称是ttt。我假设当您按F9时,只需键入要运行的函数名。比方说DIA1。这就是你应该遵循的结构。您需要添加行,您需要替换字符串DIA1等,但这是可行的。我测试了自己
Sub DIA1()
MsgBox "hello"
End Sub
Sub ttt()
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Copy
Application.Run CStr(Selection)
End Subhttps://stackoverflow.com/questions/45314781
复制相似问题