在Outlook中使用VBA使用Emojis发送文本

时间:2016-02-11 16:27:15

标签: excel vba outlook emoji

我有一些VBA代码,用于将Outlook中的文本发送给我工作的项目团队成员。对于某些背景:对于非AT& T订阅者,我们没有问题通过插入人们来从Outlook发送文本消息。数字集中到Outlook电子邮件的“收件人:”字段中。但是,所有AT& T订户都将收到该文本作为群组消息,我们希望避免这种情况。当我们进行小组发送时,非AT& T订户正确接收单独的文本。

我们编写了一些VBA代码来循环播放AT& T号码的电子表格,以便Outlook为每个AT& T号码发送一封电子邮件。这对我们来说一直很好,但是,我们希望在我们发送的文本中添加一些表情符号。我已经完成了很多谷歌搜索和搜索stackoverflows问题,我似乎无法找到为此目的而构建的任何代码。在谈到VBA时,我也是一个完整的菜鸟,我已经将这个解决方案拼凑在一起,远远没有得到同事的帮助和阅读互联网上的线索。关于表情符号的这一点给了我足够的麻烦,我以为我会分解并提交这篇文章。

供参考,这是我的代码:

Sub EmojiTest()
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim MobileNumber As String

      ' Create the Outlook session.
      Set objOutlook = CreateObject("Outlook.Application")
      Set xlApp = CreateObject("Excel.Application")

      'Grab list from Excel
      Set xlAtt = xlApp.Workbooks.Open("C:\Users\Username\Desktop\Spreadsheet with AT&T numbers.xlsx")
      xlAtt.Activate
      LastRow = xlAtt.ActiveSheet.Range("B" & xlAtt.ActiveSheet.Rows.Count).End(-4162).Row

      For i = 1 To LastRow
        xlAtt.Activate
        MobileNumber = xlAtt.ActiveSheet.Range("B" & i).Value

        ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

        objOutlookMsg.SentOnBehalfOfName = "TeamAccount@work.com"

        With objOutlookMsg
            ' Add the To recipient(s) to the message.
            Set objOutlookRecip = .Recipients.Add(MobileNumber)
            objOutlookRecip.Type = olTo

            ' Set the Subject, Body, and Importance of the message.
            .Subject = "Emoji Test"
            .Body = "Text with emojis"
            .Save
            .Send
        End With
      Next i
      Set objOutlook = Nothing
      xlApp.Workbooks.Close
      Set xlApp = Nothing
End Sub

这是我自己从未想过的代码,因为我完全缺乏VBA经验,而且编码经验有限。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

变化:

.Body = "Text with emojis"

要:

.Body = "\ud83d\ude03"

完整列表here。复制名为Java escape string的框。

\ u转义unicode序列,因此输入" \ u"并且UTF-16序列应该允许您插入任何表情符号。

有些Emojis实际上是2个单独的char序列,所以你必须把它们连在一起。

答案 1 :(得分:0)

我明白了!

Sub EmojiTest()
   Dim objOutlook As Outlook.Application
   Dim objOutlookMsg As Outlook.MailItem
   Dim objOutlookRecip As Outlook.Recipient
   Dim MobileNumber As String
   Dim strbody As String

      ' Create the Outlook session.
      Set objOutlook = CreateObject("Outlook.Application")
      Set xlApp = CreateObject("Excel.Application")

      'Grab list from Excel
      Set xlAtt = xlApp.Workbooks.Open("C:\Users\user\Desktop\Spreadsheet.xlsx")
      xlAtt.Activate
      LastRow = xlAtt.ActiveSheet.Range("B" & xlAtt.ActiveSheet.Rows.Count).End(-4162).Row

      For i = 1 To LastRow
        xlAtt.Activate
        MobileNumber = xlAtt.ActiveSheet.Range("B" & i).Value

        ' Create the message.
        Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

        objOutlookMsg.SentOnBehalfOfName = "Team@work.com"
        strbody = "<BODY style=font-size:11pt;font-family:Segoe UI Symbol>&#127881;Congrats!<p>Paragraph 2.<p>Paragraph 3.</BODY>"
        With objOutlookMsg
            ' Add the To recipient(s) to the message.
            Set objOutlookRecip = .Recipients.Add(MobileNumber)
            objOutlookRecip.Type = olTo

            ' Set the Subject, Body, and Importance of the message.
            .Subject = "Emoji Test"
            .HTMLBody = strbody


            .Save
            .Send
        End With
      Next i
      Set objOutlook = Nothing
      xlApp.Workbooks.Close
      Set xlApp = Nothing
End Sub

基本上,我必须将我的消息转换为HTML。我在顶部使用“Dim strbody As String”,然后在我的With语句中使用“.HTMLBody = strbody”。一旦我这样做,使用HTML十六进制代码输入我的表情符号是微不足道的。这是一个包含我使用的HTML十六进制代码的页面(&amp;#127881):http://www.fileformat.info/info/unicode/char/1f389/index.htm

了解了很多关于使用VBA这样做的知识,所以很有趣。

感谢你的帮助Sam。

答案 2 :(得分:0)

link处有一个解决方案。

按此方法,您将笑脸粘贴到Excel单元格中,然后读取该单元格值,它将是长度为2的字符串,使用AscW()找到这2个字符的代码,然后使用Chrw将它们链接起来,例如要笑脸可以使用ChrW(-10179)和ChrW(-8638)。