Fuggel
Fuggel

Reputation: 1

VBA WORD 2019 Redesigned comments. Looking for an Event like "Comment was added" or "Comment has changed" to autocorrect comment text afterwards

In 2019 comments in Word were redesigned. Therefore there was no autocorrection available in comments anymore. I used the autocorrection function for substituting my own abbreviations in the comments. I now wrote a VBA SUB making use of the Comments/Comment object and the AutoCorrect object. It works fine to substitute my abbreviations in all comments after I wrote them. But to get a more immediate experience, I would like to link the SUB to a "Comment was added"- or "Comment has changed"-Event but I can't find one. The closest I can get is via a call of my SUB in App_WindowSelectionChange() but the selection of a comment balloon or adding a new comment is not firing that event.

It should work like this:

editing autocorrection fu1 = fuggel1

Select: Word->Developement tools->macros-> Register_Event_Handler()

write comment including "fu1 is the best"

on event changing to "fuggel1 is the best"

Any ideas how to make the call of my SUB related to adding a new comment or changing a comment ?

Rem Class EventACC
Public WithEvents App As Word.Application

Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Rem Debug.Print ("change")
Call Auto_Correct_Comment
End Sub
Rem Module AutoCorrectComment

Dim ACC As New EventACC

Sub Register_Event_Handler()
 Set ACC.App = Word.Application
End Sub

Sub Auto_Correct_Comment()
    If ActiveDocument.Comments.Count >= 1 Then
        For X = 1 To ActiveDocument.Comments.Count
        Dim m_s_comment As String
        Dim m_s_arr_comment_p() As String
        m_s_comment = Trim(ActiveDocument.Comments(X).Range.Text)
        m_s_arr_comment_p = Split(m_s_comment, " ")
            For C = 0 To UBound(m_s_arr_comment_p)
                Rem Debug.Print (m_s_arr_comment_p(C))
                On Error Resume Next
                Dim m_s_test As String
                m_s_test = AutoCorrect.Entries(m_s_arr_comment_p(C)).Value
                If Err.Number = 0 Then
                    Rem Debug.Print (AutoCorrect.Entries(m_s_arr_comment_p(C)).Value)
                    m_s_comment = Replace(m_s_comment, m_s_arr_comment_p(C), AutoCorrect.Entries(m_s_arr_comment_p(C)).Value)
                    Rem Debug.Print (m_s_comment)
                End If
            Next C
        ActiveDocument.Comments(X).Range.Text = m_s_comment
        Next X
    End If
End Sub

Upvotes: 0

Views: 93

Answers (1)

Fuggel
Fuggel

Reputation: 1

I made some progess, now being able to change abbreviations in the selected comment via a key-shortcut (ALT + 0) after writing and confirming it or choosing the comment balloon later on. See code below. Still wanting an event related change.

Usage->Select: Word->Developement tools->macros->AddKeyBinding(). Then use the Key-Shortcut (Alt+0) on comments after writing and confirming them.

Rem Module AutoCorrectComment
Sub Auto_Correct_Comment_2()
If ActiveDocument.ActiveWindow.ActivePane.Selection.Comments.Count >= 1 Then
        m_s_comment = Trim(ActiveDocument.ActiveWindow.ActivePane.Selection.Comments.Item(1).Range.Text)
        m_s_comment_copy = m_s_comment
        Replacement = Array(".", ",", "?", "!", ":") ' add more
            For Each A In Replacement
                m_s_comment_copy = Replace(m_s_comment_copy, A, " " & A & " ") ' necessary to "free" Autocorrect Element
            Next A
        m_s_arr_comment_p = Split(m_s_comment_copy, " ")
        For C = 0 To UBound(m_s_arr_comment_p)
                    Rem Debug.Print (m_s_arr_comment_p(C))
                    On Error Resume Next
                    Dim m_s_test As String
                    m_s_test = AutoCorrect.Entries(m_s_arr_comment_p(C)).Value
                    If Err.Number = 0 Then
                        Debug.Print (m_s_arr_comment_p(C))
                        Rem Debug.Print (AutoCorrect.Entries(m_s_arr_comment_p(C)).Value)
                        m_s_comment = Replace(m_s_comment, m_s_arr_comment_p(C), AutoCorrect.Entries(m_s_arr_comment_p(C)).Value)
                        Rem Debug.Print (m_s_comment)
                    End If
                Next C
            ActiveDocument.ActiveWindow.ActivePane.Selection.Comments.Item(1).Range.Text = m_s_comment
    End If
End Sub


Sub AddKeyBinding()
    With Application
         
        .CustomizationContext = ActiveDocument.AttachedTemplate

         ' \\ Add keybinding to Active.Document Shorcut: Alt+0
        .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKey0), _
        KeyCategory:=wdKeyCategoryCommand, _
        Command:="Auto_Correct_Comment_2"
    End With
End Sub

Upvotes: 0

Related Questions