什么时候 MailItem 不是 MailItem?

发布于 2024-07-04 05:13:23 字数 1489 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

救星 2024-07-11 05:13:23

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

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

IE

Dim obj As Object

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

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

In the called procedure, assign the incoming item to an Object type variable. Then use TypeOf or TypeName to determine if it is a MailItem. Only then should your code perform actions that apply to emails.

i.e.

Dim obj As Object

If TypeName(obj) = "MailItem" Then
  ' your code for mail items here
End If
隐诗 2024-07-11 05:13:23

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

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

我会看看是否可以提供一个具体的示例是否有帮助。

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.)

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.

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

风和你 2024-07-11 05:13:23
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
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
痴情换悲伤 2024-07-11 05:13:23

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

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.

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
能怎样 2024-07-11 05:13:23

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

下面是删除了功能的代码,但让您了解它的外观:

   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

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

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

strSubject = Item.Subject

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

谢谢。

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.

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?

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?

Thanks.

落日海湾 2024-07-11 05:13:23

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

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

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

半步萧音过轻尘 2024-07-11 05:13:23

我在其他 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。

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

' 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文