【问题标题】:Run-time error 13 type mismatch reading a mailitem property of an item读取项目的 mailitem 属性的运行时错误 13 类型不匹配
【发布时间】:2020-01-19 03:04:38
【问题描述】:

我在下面的代码回复所有电子邮件节目

“运行时错误 13 类型不匹配”

当它运行到下一个时(对于每个下一个循环)。

我为今天收到的电子邮件设置了规则。当谈到下一个(下一个日期)时,它会显示错误消息。

我调试了,它停止了错误消息,直到收到的时间是今天。

sub fwdmail ()
dim i as long
dim otlk as outlook.application
dim nmspc as outlook.namespace
dim olmail as Outlook.MailItem
dim objfolder as Outlook.MAIPfolder
dim oreply as Outlook.MailItem

set otlk=New Outlook.Applicaiton
Set Nmsp=otlk.GetNamespace("MAPI")
Set objfolder=nmspc.getdefaultFolder("olFolderInbox).Folder("notice")
for each olmail in objfolder.Items
  if olmail.ReceivedTime>=Format(Date, "YYYY/MM/DD") then
    ' do the stuff here
  end if
next

我厌倦了检查是否为 mailitem 类型,但得到了相同的错误消息。

for each olmail in objfolder.Items
   if typeof olmail is outlook.item then

     if olmail.ReceivedTime>=Format(Date, "YYYY/MM/DD") then
     ' do the stuff here
      end if
   end if
next

【问题讨论】:

  • 很可能您在该文件夹中有一个不是邮件项目的项目,请参阅here
  • 我修正了问题上的错字。我也尝试检查它是否是 IF typeof 中的邮件项,但我仍然收到错误消息>
  • 你需要检查TypeOf项目之前试图循环。
  • 请注意,您可以使用Items.Restrict 而不是循环。
  • 另外 - MailItem.ReceivedTimeDate,不要将其与 Variant/String 比较,这是 Format 返回的内容。

标签: vba outlook


【解决方案1】:

我建议将接收时间的格式与您要比较的格式进行比较。

【讨论】:

  • 其他方式。删除 Format 并比较两个日期。
【解决方案2】:

声明一个允许所有类型项目的变量。测试它是否是一个邮件项。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration

Sub fwdmail()

Dim olNmspc As NameSpace
Dim olFolder As folder

Dim olItm As Object         ' <-- Items may not be mailitems
Dim olMitm As MailItem

Set olNmspc = GetNamespace("MAPI")

' Multiple lines allows easier troubleshooting
Set olFolder = olNmspc.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.folders("notice")

For Each olItm In olFolder.Items

    If TypeOf olItm Is MailItem Then    ' <-- test if item is a mailitem

        Set olMitm = olItm

        If olMitm.ReceivedTime >= Format(Date, "YYYY/MM/DD") Then
            ' do the stuff here
            Debug.Print "olMitm.Subject......: " & olMitm.Subject
            Debug.Print " olMitm.ReceivedTime: " & olMitm.ReceivedTime
            Debug.Print " Format date........: " & Format(Date, "YYYY/MM/DD")
            Debug.Print
        End If

    End If

Next

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多