我正在尝试更改每次在当前 Word 文档中按下
"
键时要输入的文本的颜色。但是 key Up
事件似乎不起作用,我什至在里面放了一个 MsgBox
看看是的,这是关键代码,什么也没有。
注意:该文档已启用宏,并且在安全选项中允许运行所有宏。
注意:我使用Office 2013
这是我的代码:
Private Sub Document_KeyPress(ByVal KeyCode As Integer, ByVal Shift As Integer)
Static Is_Str As Boolean
If KeyCode = 34 Then
' The key was pressed "
If Is_Str Then
' End Str
Selection.Font.Color = RGB(0, 0, 0)
Else
' Init Str
Selection.Font.Color = RGB(0, 255, 0)
End If
Is_Str = Not Is_Str
End If
'I put this in to make sure that if the function is called when a key is pressed
MsgBox "A key has been pressed"
End Sub
问题翻译自西班牙语:
解决此问题需要创建一个带有 WindowSelectionChange 事件的类。
插入一个类模块,其名称将是
Class1
,使用此代码
Public WithEvents myclass As Word.Application
Private Sub myclass_WindowSelectionChange(ByVal Sel As Selection)
MsgBox "A button was pressed"
End Sub
在 ThisDocument 模块中插入以下内容:
Dim X As New Class1
Sub init()
Set X.myclass = Word.Application
End Sub
运行此子程序后,如果您更改 Word 文档上的选择,即使重新定位光标,也会调用该事件。
Windows 文档:
(https://learn.microsoft.com/en-us/office/vba/api/word.application.windowselectionchange)
和