Outlook VBA-- 某些邮件项目属性返回值,其他不返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17065844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outlook VBA-- Some MailItem Properties return values, others do not
提问by Wolves
EDIT: 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.
编辑:新信息:我刚刚意识到,虽然 Mailitem.Body 的返回是 "",但实际值是 "Application-defined or object-defined error" 。我不完全确定这意味着什么,但我知道它出现在多个领域——我在下面包含了一个屏幕截图。
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 protected]", and the date sent is 12 June 2013.
我遇到了一个问题,某些属性会返回正确的值,而其他属性则不会。我有一个示例电子邮件,其中我有一封主题为“主题”的电子邮件,邮件为“正文”,发件人电子邮件地址为“[email protected]”,发送日期为 2013 年 6 月 12 日。
When I run the following code:
当我运行以下代码时:
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 protected]" Then
Computers(a, 0) = ComputerName
Computers(a, 1) = ErrorState
a = a + 1
End If
Debug.Print MailItem.Subject
Debug.Print MailItem.Body
Next 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!
我得到的是 ComputerName = "Subject"、ErrorState = ""、SenderEmail = "" 和 strDate = "2013/6/12"(在这种情况下这是正确的格式)。为什么这会为两个 Mailitem 属性返回正确的值,而不会为其他两个属性返回正确的值?这是一个非常奇怪的问题,如果您能提供任何帮助,我将不胜感激!
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 protected]/inbox")
Set InboxItems = Inbox.Items
InboxItems.SetColumns ("SentOn")
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.
GetFolder 是一个通过文件夹路径获取邮箱的函数。我必须这样做,因为我没有使用 Outlook 中的默认收件箱。
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.
我还尝试使用下面建议的 MailBody 函数,以防正文采用 HTML 或 RTF 格式。不幸的是,证明body是正常的,MailItem.Body应该已经找回了,还是不行。MailItem.Body 返回“”,即使我知道电子邮件有正文。身体只是数字 1,这就是我应该得到的。
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.
另外,我应该注意电子邮件的发件人与收件人相同;换句话说,电子邮件是从一个电子邮件地址发送给它自己的。我不知道这是否会有所作为,但我想我会把它放在那里以防万一。
回答by Joshua Honig
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:
首先,不能保证Inbox.Items
集合中的所有项目都是 类型MailItem
。收件箱还包含AppointmentItem
、MeetingItem
和其他*Item
类型的对象。并非所有这些项目类型都填充了相同的属性。为确保不会出现类型不匹配错误,请将迭代器变量声明为泛型,Object
并且仅MailItem
在类型正确时才将其分配给强类型变量:
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
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:
其次,不能保证对象的所有属性都将被填充。如果邮件项目从未发送过,它将没有发件人地址,当然也可能有一封没有正文的电子邮件。熟悉哪些属性可用以及它们包含哪些内容的一个好方法是使用 Locals 窗口(VBA IDE 中的 View > Locals Window)。以下是循环中暂停的上述代码的屏幕截图,并oMailItem
展开了对象的一些属性:
Body vs. HTMLBody
正文与 HTMLBody
MailItem
objects have three body properties: Body
, HTMLBody
, and RTFBody
. Usually only oneof 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:
MailItem
对象有三种机身特性:Body
,HTMLBody
,和RTFBody
。通常只有其中一个被填充。哪一个取决于电子邮件的格式。您可以检查该BodyFormat
属性以查找适用于当前项目的属性。使用它,无论格式如何,这里都是获取 MailItem 原始正文的通用方法:
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