博客
关于我
MSFT Outlook VBA处理新邮件的方法
阅读量:799 次
发布时间: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在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
    查看>>
    Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
    查看>>
    Mysql在Windows上离线安装与配置
    查看>>
    MySQL在渗透测试中的应用
    查看>>
    Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
    查看>>
    Mysql在离线安装时提示:error: Found option without preceding group in config file
    查看>>
    MySQL基于SSL的主从复制
    查看>>
    Mysql基本操作
    查看>>
    mysql基本操作
    查看>>
    mysql基础
    查看>>
    mysql基础---mysql查询机制
    查看>>
    MySQL基础5
    查看>>
    MySQL基础day07_mysql集群实例-MySQL 5.6
    查看>>
    Mysql基础命令 —— 数据库、数据表操作
    查看>>
    Mysql基础命令 —— 系统操作命令
    查看>>
    MySQL基础学习总结
    查看>>
    mysql基础教程三 —常见函数
    查看>>
    mysql基础教程二
    查看>>
    mysql基础教程四 --连接查询
    查看>>
    MySQL基础知识:创建MySQL数据库和表
    查看>>