Outlook VBA-一些MailItem属性返回值,另一些则不 [英] Outlook VBA-- Some MailItem Properties return values, others do not

查看:535
本文介绍了Outlook VBA-一些MailItem属性返回值,另一些则不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新信息:我刚刚意识到,尽管Mailitem.Body的返回为",但实际值为应用程序定义的错误或对象定义的错误".我不确定是什么意思,但是我确实知道它会显示在多个字段中-我在下面提供了一个屏幕截图.

New info: I just now realised that, while the return of Mailitem.Body is "", the actual value is "Application-defined or object-defined error" . I'm not entirely sure what that means, but I do know it shows up in multiple fields-- I included a screen shot below.

我遇到一个问题,其中某些属性将返回正确的值,而其他属性则不能.我有一个示例电子邮件,其中有一个主题为主题"的电子邮件,消息为正文",发件人电子邮件地址为"email@address.com",发送日期为2013年6月12日.

I am having an issue where certain properties will return the correct value, and others will not. I have an example email, where I have an email with subject "Subject", the message is "Body", the sender email address is "email@address.com", and the date sent is 12 June 2013.

当我运行以下代码时:

    Dim ComputerName As String
    Dim ErrorState As String
For Each MailItem In InboxItems
        ComputerName = MailItem.Subject
        'ErrorState = MailItem.Body
        ErrorState = MailBody(MailItem)
        strDate = GetDate(MailItem.SentOn)
        SenderEmail = MailItem.SenderEmailAddress
        If strDate = DateToday And SenderEmail = "email@address.com" Then
            Computers(a, 0) = ComputerName
            Computers(a, 1) = ErrorState
            a = a + 1
        End If
        Debug.Print MailItem.Subject
        Debug.Print MailItem.Body
    Next MailItem

我得到的是ComputerName ="Subject",ErrorState =",SenderEmail ="和strDate ="2013/6/12"(在这种情况下,这是正确的格式).为什么这会为两个Mailitem属性返回正确的值,而不为其他两个属性返回正确的值?这是一个非常奇怪的问题,如果您能提供所有帮助,我将不胜感激!

What I get is ComputerName = "Subject", ErrorState = "", SenderEmail = "", and strDate = "2013/6/12" (which is the proper format in this case). Why would this return proper values for two of the Mailitem properties, but not for two of the others? This is a very strange problem, and I would appreciate any help you all might be able to give!

我将在此处为代码添加更多上下文:

I will add more of the context for the code here:

    Set objOutlook = CreateObject("Outlook.Application", "localhost")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set Inbox = GetFolder("email@address.org/inbox")
    Set InboxItems = Inbox.Items
    InboxItems.SetColumns ("SentOn")

GetFolder是通过文件夹路径获取邮箱的功能.我必须这样做,因为我没有在Outlook中使用默认收件箱.

GetFolder is a function to get the mailbox by folder path. I have to do this because I am not using the default inbox in outlook.

如果正文为HTML或RTF格式,我还尝试使用下面提出的MailBody函数.不幸的是,事实证明该正文是正常的,MailItem.Body应该已经检索了它,但它仍然无法正常工作.即使我知道电子邮件具有正文,MailItem.Body也会返回".身体只是数字1,这就是我应该得到的.

I also tried using the MailBody Function proposed below, in case the body were in an HTML or RTF format. Unfortunately, it proved that the body was normal, and MailItem.Body should have retrieved it, and it still is not working. MailItem.Body returns "", even though I know that the email has a body. The body is just the number 1, and that is what I should be getting.

另外,我应该注意,电子邮件的发件人与收件人相同;换句话说,电子邮件是从一个电子邮件地址发送给自己的.我不知道这是否可以有所作为,但我认为我会把它放在那里,以防万一.

Also, I should note that the sender of the email is the same as the recipient; in other words, the email was sent from one email address to itself. I don't know if this could make a difference, but I figured that I would put it out there just in case.

推荐答案

多种商品类型

首先,不能保证Inbox.Items集合中的所有项目都是MailItem类型.收件箱还包含AppointmentItemMeetingItem和其他*Item类型的对象.并非所有这些项目类型都具有相同的属性.为了确保不会出现类型不匹配错误,请将迭代器变量声明为通用Object,并且仅在类型正确的情况下才将其分配给强类型的MailItem变量:

Multiple Item Types

First, there is no guarantee that all items in the Inbox.Items collection are of type MailItem. Inboxes also contain AppointmentItem, MeetingItem, and other *Item type objects. Not all of these item types have the same properties populated. To ensure you do not get a type mismatch error, declare your iterator variable as a generic Object and only assign it to a strongly-typed MailItem variable if it is of the correct type:

Dim oInbox    As Outlook.Folder
Dim oItem     As Object
Dim oMailItem As MailItem

Set oInbox = ActiveExplorer.Session.DefaultStore.GetRootFolder().Folders("Inbox")
For Each oItem In oInbox.Items
    If TypeOf oItem Is MailItem Then
        Set oMailItem = oItem
        ' Do stuff
    Else
        Debug.Print "Skipping " & TypeName(oItem)
    End If
Next

可选属性

第二,没有保证将填充对象的所有属性.如果从未发送过邮件,则邮件将没有发件人地址,并且肯定会有一封没有正文的电子邮件.熟悉哪些可用属性以及它们包含的属性的一种好方法是使用本地"窗口(VBA IDE中的视图">本地"窗口).这是上述代码在循环中暂停的屏幕截图,其中扩展了oMailItem对象的某些属性:

Optional properties

Second, there is no gaurantee that all properties of an object will be populated. If a mail item was never sent, it will have no sender address, and certainly it is possible to have an email with no body. A good way to get familiar with which properties are available and what they contain is to use the Locals window (View > Locals Window in the VBA IDE). Here's a screen shot of the above code paused in the loop, with some of the properties of the oMailItem object expanded:

MailItem 对象具有三个主体属性: Body BodyFormat 属性以查找哪个是适用于当前项目.使用这种方法,这是一种获取MailItem原始内容的通用方法,无论采用哪种格式:

MailItem objects have three body properties: Body, HTMLBody, and RTFBody. Usually only one of them is populated. Which one depends on the format of the email. You can check the BodyFormat property to find which one is applicable to the current item. Using that, here's a generalized way to get the raw body of a MailItem, no matter what the format:

Public Function MailBody(ByVal MailItem As MailItem) As String
    Select Case MailItem.BodyFormat
        Case OlBodyFormat.olFormatPlain, OlBodyFormat.olFormatUnspecified
            MailBody = MailItem.Body
        Case OlBodyFormat.olFormatHTML
            MailBody = MailItem.HTMLBody
        Case OlBodyFormat.olFormatRichText
            MailBody = MailItem.RTFBody
    End Select
End Function

这篇关于Outlook VBA-一些MailItem属性返回值,另一些则不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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