对象不支持

时间:2011-09-07 13:07:13

标签: vba excel-vba excel-2003 outlook-vba excel

我正在浏览以下代码,但它似乎无法在我的计算机上运行。它给出了一个错误:格式不支持对象(Item.ReceivedTime,“MMDDYYYY”)=格式(现在,“MMDDYYYY”)部分。

我在收件箱中创建了一个名为ELN的文件夹,并在那里放了一个带有.xls文件的电子邮件,但它仍然无效。我在Excel.Any建议中添加了DOA和Outlook引用?

Dim appOl As New Outlook.Application
Dim ns As Outlook.Namespace
Dim Inbox As Outlook.MAPIFolder
Dim Atmt As Outlook.Attachment
Dim SubFolder As Outlook.MAPIFolder
Dim Item As Object
Dim FileName As String
Dim i As Integer
Dim varResponse As VbMsgBoxResult

Set ns = appOl.GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("ELN")
i = 0

For Each Item In SubFolder.Items
  If Format(Item.ReceivedTime, "MMDDYYYY") = Format(Now, "MMDDYYYY") Then
    For Each Atmt In Item.Attachments

        If Right(Atmt.FileName, 3) = "xls" Then
            FileName = "SomeFile.xls"
            Atmt.SaveAsFile FileName
            smkSubject = Item.Subject
            i = i + 1
        End If
    Next Atmt
  End If
Next Item

1 个答案:

答案 0 :(得分:1)

“item”可能是也可能不是邮件。

这可能会起到作用:

For Each Item In Inbox.Items
        If Item.Class = olMail Then 'Make sure it's a mail item
            If Format(Item.ReceivedTime, "MMDDYYYY") = Format(Now, "MMDDYYYY") Then
                For Each Atmt In Item.Attachments
                    If Right(Atmt.FileName, 3) = "doc" Then
                        FileName = "SomeFile.xls"
                        Atmt.SaveAsFile FileName
                        smkSubject = Item.Subject
                        i = i + 1
                    End If
                Next Atmt
            End If
        Else
            Debug.Print "This is not a mail item:" & Item.Class 'Some class constant that's not supported as a mail item.
        End If
    Next Item