首页 > 解决方案 > 为正文中的联系人创建带有@Mention 的 Outlook 电子邮件

问题描述

我正在通过 Excel VBA 生成具有 HTML 正文的 Outlook 电子邮件。

我想@我组织中的一个联系人。例如@Bloggs,乔

Mention是最近几年添加到 Outlook 的一项功能,它似乎是一个 HTML 链接。如何确保 Outlook 的格式mention正确?

以下内容是否足以让 Outlook 将其识别为提及?

<a href="mailto:Joe.Bloggs@MyOrg.com">
<span style='font-family:"Arial",sans-serif;mso-no-proof:yes;text-decoration:none; text-underline:none'>@Bloggs, Joe</span>

标签: htmlexcelvbaoutlook

解决方案


诀窍似乎在id锚标签中:

  • 每个 ID 都需要以开头OWAAM(我怀疑它代表“Outlook Web Access At Mention”,因为这是该功能的起源),然后是 32 个字符的十六进制值,然后是Z.
  • 每个 ID 在文档中都必须是唯一的,但对于提及而言不必是唯一的,因此您可以为每次提及生成一个新 ID。

例如:

<a id="OWAAM954ABFF4E42A42989C2192D3F93BB32DZ" 
   href="mailto:immo@example.com">Immo Landwerth</a>

此代码适用于我(C#):

static string GetMentionHtml(string email, string name)
{
    var guid = Guid.NewGuid().ToString("N").ToUpper();
    var id = $"OWAAM{guid}Z";
    return = $"<a id=\"{id}\" href=\"mailto:{email}\">@{name}</a>";
}

如果您在 VBA 中需要它:

Function GetMentionHtml(email As String, name As String) As String
    Dim id As String
    Dim html As String    
    id = "OWAAM" & NewGuid() & "Z"
    html = "<a id=""" & id & """ href=""mailto:" & email & """>@" & name & "</a>"
    GetMentionHtml = html
End Function

' Sadly there is no "get me a GUID" in VBA, but this is good enough.
' Source: https://www.codegrepper.com/code-examples/vb/excel+vba+generate+guid+uuid
Function NewGuid() As String
    Dim k&, h$
    NewGuid = Space(36)
    For k = 1 To Len(NewGuid)
        Randomize
        Select Case k
            Case 9, 14, 19, 24: h = "-"
            Case 15:            h = "4"
            Case 20:            h = Hex(Rnd * 3 + 8)
            Case Else:          h = Hex(Rnd * 15)
        End Select
        Mid$(NewGuid, k, 1) = h
    Next
    NewGuid = Replace(NewGuid, "-", vbNullString, Compare:=vbTextCompare)
End Function

推荐阅读