'Use VBA to @-Mentions a user in a Threaded Comment in excel

I added a UserForm into my file so comments can be added to a cell (that way I can update the data on other cells when a comment is added and someone is mentioned).

So far I can get the comment entered with no issues. But I can´t find a way to @mention a user so the notification is sent. Does anyone know if this is manageable with VBA?

Range("A1").AddCommentThreaded ("Comment text")


Solution 1:[1]

Answer
By reading the documentation the method is not likely implemented in VBA and seems only a front end to Excel, but not visible to VBA itself. The only property that I found was "resolved" (which is not mentioned in the documentation of the object itself), but there is not a way to "resolve" it per say.

enter image description here

VBA does not resolve the user (even if it's correctly written) and most likely there is no native way to do so.

Workaroud
Your only solution would be to implement it by yourself: according to your question, since you are using an UserForm I would append something like this

Add the reference for outlook (you may use late binding, but I rather to add the references as it is better IMHO)

enter image description here

In a module, add the following:

Function Return_TxtFoundContact() As String
Dim ObjNamesDialog As Outlook.SelectNamesDialog
Set ObjNamesDialog = Outlook.Session.GetSelectNamesDialog
Dim ObjAddressEntry As Outlook.AddressEntry
    With ObjNamesDialog ' 1. With ObjNamesDialog
    .Caption = "Select contact to mention & notify": .ToLabel = "Mention:"
    .NumberOfRecipientSelectors = olShowTo: .AllowMultipleSelection = False 'although with this setting it lets user to select more than one recipient
    If .Display Then ' 1. If .Display
    TxtEntryID = .Recipients(1).EntryID: Set ObjAddressEntry = Outlook.Application.Session.GetAddressEntryFromID(TxtEntryID)
    Return_TxtFoundContact = ObjAddressEntry.GetExchangeUser.PrimarySmtpAddress
    End If ' 1. If .Display
    End With ' 1. With ObjNamesDialog
    Set ObjAddressEntry = Nothing: Set ObjNamesDialog = Nothing
End Function
Sub Test()
    Call Exec_SendNotificationMentionMail("[email protected]", Range("E4"))
End Sub
Sub Exec_SendNotificationMentionMail(TxtEmailToSendTo As String, RangeCommentIs As Range)
Dim AppOutlook As Outlook.Application
Set AppOutlook = New Outlook.Application
Dim ObjMailItem As Outlook.MailItem: Set ObjMailItem = AppOutlook.CreateItem(olMailItem)
    With ObjMailItem
    ObjMailItem.To = TxtEmailToSendTo
    'since you may have many users under outlook, I rather to get the application username, however you may go to https://docs.microsoft.com/en-us/office/vba/api/outlook.namespace.currentuser
    'to see how to get the username by outlook or use Environ("Username"), varies per needs/company to get the desired outcome
    ObjMailItem.Subject = Application.UserName & " mentioned you in '" & ThisWorkbook.Name & "'"
    'If you wish, format it as microsoft would do, just research on how to give format to the htmlbody on outlook, for simplicity I just add the basic
    ObjMailItem.HTMLBody = Application.UserName & " mentioned you at: " & RangeCommentIs.Address(False, False) & Chr(10) & RangeCommentIs.CommentThreaded.Text
    'for debug purposes, display it, once you have verified it works as you would like, comment the line
    .Display
    'Once you have verified it works as intended, uncomment this
    '.Send
    End With
    'Once you have verified it works as intended, uncomment this
    'Set ObjMailItem = Nothing: Set AppOutlook = Nothing
End Sub

In your userform, add a textbox where, upon double clicking, user agenda (per code above) would show up to select from the directory the person being mentioned

enter image description here

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim TxtFoundContact As String
    TxtFoundContact = Return_TxtFoundContact
    TextBox1 = TxtFoundContact
End Sub

enter image description here

Finally, on your userform implementation when they hit "OK" or when your userform appends the comment to a mail and send it using the routine.

enter image description here

enter image description here

OT: This method may be more useful than the actual one, you may select users that the workbook has not been shared with, if they get mentioned, but they do not have access yet, they can request it (I think the communication process will be faster with this). I am not quite sure if the original implementation allows it, but if needed, multiple people can be notified under the same mail too, you just need to adjust the code above to do so.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1