博客
关于我
MSFT Outlook VBA处理新邮件的方法
阅读量:793 次
发布时间:2023-02-10

本文共 2402 字,大约阅读时间需要 8 分钟。

Outlook邮件自动转发处理脚本

背景与需求

在我们的工作环境中,使用了两种邮件客户端:外部邮箱(Outlook)和内部邮箱(Lotus Notes)。为了提高工作效率,我们希望在收到Outlook邮箱中的新邮件时,自动判断邮件的主题内容。如果邮件主题以“kkk:"开头,则将“kkk:"后面的内容提取出来,并将其作为收件人地址发送到Lotus Notes邮箱中。

解决方案

为了实现上述功能,我们可以使用微软Outlook的VBA宏编写一个自动化脚本。以下是实现该功能的详细代码和说明。

代码实现

Option ExplicitPublic WithEvents outApp As Outlook.ApplicationSub Initialize_handle()    Set outApp = ApplicationEnd Sub' 打开OutLook的时候调用,注册application引用Private Sub Application_Startup()    Initialize_handleEnd Sub' 收到新邮件的时候自动调用Private Sub outApp_NewMailEx(ByVal EntryIDCollection As String)    Dim mai As Object    Dim intInitial As Integer    Dim intFinal As Integer    Dim strEntry As String    Dim intLength As Integer    intInitial = 1    intLength = Len(EntryIDCollection)    intFinal = InStr(intInitial, EntryIDCollection, ",")        Do While intFinal > 0        strEntryID = StringMid(EntryIDCollection, intInitial, (intFinal - intInitial))        Set mai = Application.Session.GetItemFromID(strEntryID)        newmail_proc mai        intInitial = intFinal + 1        intFinal = InStr(intInitial, EntryIDCollection, ",")    Loop    strEntryID = StringMid(EntryIDCollection, intInitial, (intLength - intInitial) + 1)    Set mai = Application.Session.GetItemFromID(strEntryID)    newmail_proc maiEnd SubPrivate Sub newmail_proc(ByVal mai As Object)    Dim itm As Object    Dim result As Integer    Dim str_kkk As String    Dim str_subject As String    Dim len_subject As Integer    Dim str_body As String    Dim str_reception As String    str_subject = mai.subject    len_subject = Len(str_subject)        str_kkk = StringMid(str_subject, 1, 4)    result = StringStrComp(str_kkk, "kkk:", vbTextCompare)        If result > 0 Then        str_reception = StringMid(str_subject, 5, (len_subject - 4) + 1)        str_body = mai.body        Set itm = outApp.CreateItem(0)        With itm            subject = "新邮件来自a@a.com"            to = str_reception            body = str_body            send        End With    End IfEnd Sub

代码解释

  • 初始化设置:在Outlook启动时,注册一个引用,并初始化相关变量。
  • 新邮件处理:当收到新邮件时,通过NewMailEx事件处理,提取邮件的ID集合。
  • 邮件解析:解析每个邮件的ID,获取对应邮件对象进行处理。
  • 主题检查:检查邮件主题是否以“kkk:"开头。
  • 信息提取与转发:如果主题以“kkk:"开头,提取后续内容并转发到Lotus Notes邮箱。
  • 实施步骤

  • 打开Outlook,点击菜单栏的“工具” -> “宏” -> “安全性”,将宏的安全性设置为“低”。
  • 将以上代码粘贴到新的模块中,确保Option Explicit已设置。
  • 保存并关闭编辑器。
  • 注意事项

    • 代码中outApp引用需手动添加,确保在References中包含Outlook对象库。
    • 代码需放在正确的模块中,避免导致脚本无法运行。

    通过以上实现,我们可以自动处理Outlook收到的邮件,实现主题匹配后自动转发到Lotus Notes邮箱的功能,提升工作效率。

    转载地址:http://iaffk.baihongyu.com/

    你可能感兴趣的文章
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>
    multiprocessing.pool.map 和带有两个参数的函数
    查看>>
    MYSQL CONCAT函数
    查看>>
    multiprocessing.Pool:map_async 和 imap 有什么区别?
    查看>>
    MySQL Connector/Net 句柄泄露
    查看>>
    multiprocessor(中)
    查看>>
    mysql CPU使用率过高的一次处理经历
    查看>>
    Multisim中555定时器使用技巧
    查看>>
    MySQL CRUD 数据表基础操作实战
    查看>>