使用 VBA 阅读新的 Outlook 电子邮件? [英] Using VBA to read new Outlook Email?

查看:48
本文介绍了使用 VBA 阅读新的 Outlook 电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码告诉新消息何时到达!

I have the following code which tells when new message has arrived!

Private Sub Application_NewMail()
    MsgBox "New Mail Has Arrived"
End Sub

我如何阅读这封邮件的正文和主题?有没有好的outlook编程教程?

How do I read the body,subject of this mail? Are there any good tutorials for outlook programming?

我发现 msdn 教程很有用,但是概述.

I found msdn tutorial which was useful but was general overview.

推荐答案

你需要这样的东西:

Private WithEvents myOlItems  As Outlook.Items

Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
      Set olApp = Outlook.Application
      Set objNS = olApp.GetNamespace("MAPI")
      Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem

  If TypeName(item) = "MailItem" Then
    Set Msg = item

    MsgBox Msg.Subject
    MsgBox Msg.Body

  End If

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

将代码粘贴到 ThisOutlookSession 并重新启动 Outlook.当邮件进入您的默认本地收件箱时,您会看到带有主题和正文的弹出窗口.

Paste the code into ThisOutlookSession and restart Outlook. When a message enters your default local Inbox you'll see the popup with subject and body.

这篇关于使用 VBA 阅读新的 Outlook 电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆