vba 什么时候 MailItem 不是 MailItem?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/78924/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:19:49  来源:igfitidea点击:

When is a MailItem not a MailItem?

vbaoutlook-vbatypeofoutlook-2003mailitem

提问by Killian Tyler

I have written a message handler function in Outlook's Visual Basic (we're using Outlook 2003 and Exchange Server) to help me sort out incoming email.

我在 Outlook 的 Visual Basic(我们使用的是 Outlook 2003 和 Exchange Server)中编写了一个消息处理函数来帮助我整理传入的电子邮件。

It is working for me, except sometimes the rule fails and Outlook deactivates it.

它对我有用,除了有时规则失败并且 Outlook 停用它。

Then I turn the rule back on and manually run it on my Inbox to catch up. The rule spontaneously fails and deactivates several times a day.

然后我重新打开规则并在我的收件箱上手动运行它以赶上。该规则自发地失败并每天停用数次。

I would love to fix this once and for all.

我很想一劳永逸地解决这个问题。

回答by Killian Tyler

This code showed me the different TypeNames that were in my Inbox:

此代码向我展示了收件箱中的不同类型名称:

Public Sub GetTypeNamesInbox()
Dim myOlItems As Outlook.Items
Set myOlItems = application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
Dim msg As Object

For Each msg In myOlItems
    Debug.Print TypeName(msg)
    'emails are typename MailItem
    'Meeting responses are typename MeetingItem
    'Delivery receipts are typename ReportItem
Next msg

End Sub

HTH

HTH

回答by Bruce E. Leandro

I use the following VBA code snippet in other Office Applications, where the Outlook Library is directly referenced.

我在其他 Office 应用程序中使用了以下 VBA 代码片段,其中直接引用了 Outlook 库。

' Outlook Variables

  Dim objOutlook As Outlook.Application: Set objOutlook = New Outlook.Application
  Dim objNameSpace As Outlook.NameSpace: Set objNameSpace = objOutlook.GetNamespace("MAPI")
  Dim objFolder As MAPIFolder: Set objFolder = objNameSpace.PickFolder()
  Dim objMailItem As Outlook.MailItem

  Dim iCounter As Integer:  iCounter = objFolder.Items.Count
  Dim i As Integer

  For i = iCounter To 1 Step -1
    If TypeOf objFolder.Items(i) Is MailItem Then
      Set objMailItem = objFolder.Items(i)
      With objMailItem

etc.

等等。

回答by Bruce E. Leandro

have written a message handler function in Outlook's Visual Basic (we're using Outlook 2003 and Exchange Server) to help me sort out incoming email. It is working for me, except sometimes the rule fails and Outlook deactivates it. Then I turn the rule back on and manually run it on my Inbox to catch up. The rule spontaneously fails and deactivates several times a day. I would love to fix this once and for all.

我在 Outlook 的 Visual Basic 中编写了一个消息处理函数(我们使用的是 Outlook 2003 和 Exchange Server)来帮助我整理传入的电子邮件。它对我有用,除了有时规则失败并且 Outlook 停用它。然后我重新打开规则并在我的收件箱上手动运行它以赶上。该规则自发地失败并每天停用数次。我很想一劳永逸地解决这个问题。

Here is the code stripped of the functionality, but giving you an idea of how it looks:

这是去除了功能的代码,但让您了解它的外观:

   Public WithEvents myOlItems As Outlook.Items

   Public Sub Application_Startup()
       ' Reference the items in the Inbox. Because myOlItems is declared
       ' "WithEvents" the ItemAdd event will fire below.
       ' Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
       Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
   End Sub

   Private Sub myOlItems_ItemAdd(ByVal Item As Object)
       On Error Resume Next
       If TypeName(Item) = "MailItem" Then
           MyMessageHandler Item
       End If
   End Sub

   Public Sub MyMessageHandler(ByRef Item As MailItem)
       Dim strSender As String
       Dim strSubject As String

       If TypeName(Item) <> "MailItem" Then
           Exit Sub
       End If

       strSender = LCase(Item.SenderEmailAddress)
       strSubject = Item.Subject

       rem do stuff
       rem do stuff
       rem do stuff
   End Sub

One error I get is "Type Mismatch" calling MyMessageHandler where VB complains that Item is not a MailItem. Okay, but TypeName(Item) returns "MailItem", so how come Item is not a MailItem?

我得到的一个错误是“类型不匹配”调用 MyMessageHandler,其中 VB 抱怨 Item 不是 MailItem。好的,但是 TypeName(Item) 返回“MailItem”,那么为什么 Item 不是 MailItem?

Another one I get is where an email with an empty subject comes along. The line

我收到的另一个邮件是一封主题为空的电子邮件。线

strSubject = Item.Subject

gives me an error. I know Item.Subject should be blank, but why is that an error?

给我一个错误。我知道 Item.Subject 应该是空白的,但为什么这是一个错误?

Thanks.

谢谢。

回答by Dave DuPlantis

My memory is somewhat cloudy on this, but I believe that a MailItem is not a MailItem when it is something like a read receipt. (Unfortunately, the VBA code that demonstrated this was written at another job and isn't around now.)

我的记忆对此有些模糊,但我相信 MailItem 当它类似于已读回执时就不是 MailItem。(不幸的是,演示这一点的 VBA 代码是在另一份工作中编写的,现在不存在了。)

I also had code written to process incoming messages, probably for the same reason you did (too many rules for Exchange, or rules too complex for the Rules Wizard), and seem to recall running into the same problem you have, that some items seemed to be from a different type even though I was catching them with something like what you wrote.

我还编写了处理传入消息的代码,可能出于与您相同的原因(Exchange 规则太多,或者规则向导的规则太复杂),并且似乎记得遇到了同样的问题,有些项目似乎来自不同的类型,即使我用你写的东西来捕捉它们。

I'll see if I can produce a specific example if it will help.

如果有帮助,我会看看我是否可以提供一个具体的例子。

回答by JimmyPena

There are many types of items that can be seen in the default Inbox.

在默认收件箱中可以看到多种类型的项目。

In the called procedure, assign the incoming item to an Objecttype variable. Then use TypeOfor TypeNameto determine if it is a MailItem. Only then should your code perform actions that apply to emails.

在被调用的过程中,将传入的项目分配给一个Object类型变量。然后使用TypeOfTypeName来确定它是否是MailItem. 只有这样,您的代码才能执行适用于电子邮件的操作。

i.e.

IE

Dim obj As Object

If TypeName(obj) = "MailItem" Then
  ' your code for mail items here
End If

回答by Radek

Dim objInboxFolder As MAPIFolder
Dim oItem As MailItem
Set objInboxFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

For Each Item In objInboxFolder.Items
    If TypeName(Item) = "MailItem" Then
    Set oItem = Item

next

回答by htd

why not use a simple error handler for the code? Seriously. You could write an error for each read of a property or object that seems to fail. Then have it Resume no matter what. No need for complex error handling. Think of a test that shows an empty subject. Since you don't know what value it will return, if any, and it seems to error on an empty or blank subject, you need to picture it as a simple test with a possible error. Run the test as an if statement (one in which you will get an error anyway), and have the program resume on error.

为什么不对代码使用简单的错误处理程序?严重地。您可以为每次读取似乎失败的属性或对象写入错误。然后无论如何都要恢复。不需要复杂的错误处理。想想一个显示空主题的测试。由于您不知道它将返回什么值(如果有),并且它似乎在一个空或空白的主题上出错,因此您需要将其描述为一个可能存在错误的简单测试。将测试作为 if 语句运行(无论如何都会出错),并让程序在出错时恢复。

On Error Resume Next
If object.subject = Null 'produces an error when subject is null, otherwise allows a good read
  strSubject = ""   'sets the subject grab string to a null or empty string as a string
Else
 strSubject = object.subject 'Sets the subject grab string to the subject of the message\item
End If