【问题标题】:Error 438 When Saving Attachments using Outlook VBA使用 Outlook VBA 保存附件时出现错误 438
【发布时间】:2020-03-07 16:13:40
【问题描述】:

我将这些拼凑在一起,以便将收到的邮件中的所有 Excel 附件保存到本地驱动器文件夹中。

它在 ThisOutlookSession 模块中,我重新启动了 Outlook。

当我发送符合 If 语句中条件的测试电子邮件时,我收到 >“错误 438:对象不支持此属性或方法”。

我不知道哪个对象不支持哪个属性或方法。

它至少符合我的 If 语句,因为这只发生在符合条件的电子邮件中。

Option Explicit

Private WithEvents inboxItems As Outlook.Items

Private Sub Application_Startup()
    Dim outlookApp As Outlook.Application
    Dim objectNS As Outlook.NameSpace
    Set outlookApp = Outlook.Application
    Set objectNS = outlookApp.GetNamespace("MAPI")
    Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem

Dim i As Integer
Dim strFolder As String
Dim mySaveName As String
Dim myExt As String
Dim OlMail As Outlook.MailItem

strFolder = "D:\Scripts\VendorProductivity\Daily files"

If TypeName(Item) = "MailItem" Then
    If Item.Subject Like "*Report*" Then
        If Item.Recipient = "Jane Doe" Then
            If Item.Attachments.Count > 0 Then

                'loop through all attachments
                For i = 1 To Item.Attachments.Count

                   mySaveName = Item.Attachments.Item(i).FileName
                   myExt = Split(mySaveName, ".")(1)

                   'Only save files with named extensions
                   Select Case myExt
                       Case "xls", "xlsm", "xlsx"
                           mySaveName = strFolder & "\" & mySaveName
                           Item.Attachments.Item(i).SaveAsFile mySaveName

                       Case Else
                           'do nothing
                   End Select
                Next
                Item.Delete
            End If
        End If
    End If
End If

ExitNewItem:
    Exit Sub

ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ExitNewItem
End Sub

【问题讨论】:

  • 哪个语句给出了错误?
  • 请删除On Error GoTo ErrorHandler,这将隐藏导致错误的语句。

标签: vba outlook


【解决方案1】:

MailItem 不公开名为Recipient(单数)的属性。它公开了一个名为Recipients(复数)的属性,但它不是字符串属性——它是Recipient 对象的集合,这些对象公开了NameAddress 等属性。

您的意思是改用SenderName 属性吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    相关资源
    最近更新 更多