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

查看:1983
本文介绍了使用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天全站免登陆