gpt4 book ai didi

excel - 使用 VBA 在 excel 中的线程评论中@-提及用户

转载 作者:行者123 更新时间:2023-12-04 20:07:14 26 4
gpt4 key购买 nike

我在我的文件中添加了一个用户窗体,因此可以将评论添加到单元格中(这样我可以在添加评论并提及某人时更新其他单元格上的数据)。
到目前为止,我可以毫无问题地输入评论。但是我找不到@提及用户的方法,因此发送了通知。有谁知道这是否可以使用 VBA 进行管理?

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

最佳答案

回答
通过阅读 documentation该方法不太可能在 VBA 中实现,并且似乎只是 Excel 的前端,但对 VBA 本身不可见。我发现的唯一属性是“已解决”(在对象本身的 documentation 中未提及),但没有办法按说法“解决”它。
enter image description here
VBA 不会解析用户(即使它是正确编写的),并且很可能没有本地方法可以这样做。
解决方法
您唯一的解决方案是自己实现它:根据您的问题,由于您使用的是 UserForm 我会附加这样的内容
添加outlook的引用(您可以使用后期绑定(bind),但我宁愿添加引用,因为它更好恕我直言)
enter image description here
在一个模块中,添加以下内容:

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
在您的用户表单中,添加一个文本框,双击后,用户议程(根据上面的代码)将显示以从目录中选择被提及的人
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
最后,在您的用户表单实现中,当他们点击“确定”或您的用户表单将评论附加到邮件并使用例程发送时。
enter image description here
enter image description here
OT:这个方法可能比实际的更有用,你可以选择没有共享工作簿的用户,如果他们被提及,但他们还没有访问权限,他们可以请求它(我认为通信过程会更快)。我不太确定原始实现是否允许,但如果需要,也可以在同一邮件下通知多个人,您只需调整上面的代码即可。

关于excel - 使用 VBA 在 excel 中的线程评论中@-提及用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72219966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com