使用 VBA 在 excel 的线程评论中 @-提及用户
Use VBA to @-Mentions a user in a Threaded Comment in excel
我在我的文件中添加了一个用户窗体,以便可以将评论添加到一个单元格(这样我可以在添加评论和提到某人时更新其他单元格上的数据)。
到目前为止,我可以毫无问题地输入评论。但是我找不到@mention 用户的方法,所以通知被发送了。有谁知道这是否可以用 VBA 来管理?
Range("A1").AddCommentThreaded ("Comment text")
回答
通过读取对象本身的 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),但没有办法“解决”它。
VBA 不解析用户(即使它写得正确)并且很可能没有本地方法可以这样做。
解决方法
你唯一的解决办法是自己实现它:根据你的问题,因为你使用的是用户窗体,我会附加这样的东西
添加 outlook 的引用(您可以使用后期绑定,但我宁愿添加引用,因为恕我直言,这样更好)
在模块中,添加以下内容:
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("sample@domain.com", 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
在您的用户表单中,添加一个文本框,双击后,用户议程(根据上面的代码)将显示被提及的人目录中的 select
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim TxtFoundContact As String
TxtFoundContact = Return_TxtFoundContact
TextBox1 = TxtFoundContact
End Sub
最后,当用户点击“确定”或当您的用户表单将评论附加到邮件并使用例程发送时,在您的用户表单实现上。
OT:此方法可能比实际方法更有用,您可能 select 工作簿尚未与之共享的用户,如果他们被提及,但他们还没有访问权限,他们可以请求它(我认为沟通过程会更快)。我不太确定原来的实现是否允许,但如果需要,也可以在同一封邮件下通知多人,你只需要调整上面的代码即可。
我在我的文件中添加了一个用户窗体,以便可以将评论添加到一个单元格(这样我可以在添加评论和提到某人时更新其他单元格上的数据)。
到目前为止,我可以毫无问题地输入评论。但是我找不到@mention 用户的方法,所以通知被发送了。有谁知道这是否可以用 VBA 来管理?
Range("A1").AddCommentThreaded ("Comment text")
回答
通过读取对象本身的 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),但没有办法“解决”它。
VBA 不解析用户(即使它写得正确)并且很可能没有本地方法可以这样做。
解决方法
你唯一的解决办法是自己实现它:根据你的问题,因为你使用的是用户窗体,我会附加这样的东西
添加 outlook 的引用(您可以使用后期绑定,但我宁愿添加引用,因为恕我直言,这样更好)
在模块中,添加以下内容:
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("sample@domain.com", 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
在您的用户表单中,添加一个文本框,双击后,用户议程(根据上面的代码)将显示被提及的人目录中的 select
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim TxtFoundContact As String
TxtFoundContact = Return_TxtFoundContact
TextBox1 = TxtFoundContact
End Sub
最后,当用户点击“确定”或当您的用户表单将评论附加到邮件并使用例程发送时,在您的用户表单实现上。
OT:此方法可能比实际方法更有用,您可能 select 工作簿尚未与之共享的用户,如果他们被提及,但他们还没有访问权限,他们可以请求它(我认为沟通过程会更快)。我不太确定原来的实现是否允许,但如果需要,也可以在同一封邮件下通知多人,你只需要调整上面的代码即可。