excel - 根据主题使用vba下载outlook附件在其他计算机上不起作用

标签 excel vba outlook office-automation com-automation

该代码从 Excel 中的“A”列中读取行,在 Outlook 中查找它们并将附件下载到范围(“E3”)中的文件夹,该文件夹会根据计算机而变化。
但是,在我的电脑上它运行良好,在我同事的电脑上它不下载文件。

Sub Descarga()
    Dim it, at      As Variant, t As Long
    Dim olApp       As Outlook.Application
    Dim olNS        As Outlook.Namespace
    Dim olFolder    As Outlook.MAPIFolder
    Dim olItem      As Object
    Dim mailitem    As Outlook.mailitem
    Dim olAtt       As Outlook.Attachment
    
    Set olApp = New Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olFolder = olNS.GetDefaultFolder(olFolderInbox)

    For Each it In CreateObject("outlook.application").GetNamespace("MAPI").GetDefaultFolder(6).Items
        For t = 1 To Range("A" & Rows.Count).End(xlUp).Row
            If InStr(it.Subject, Cells(t, 1)) Then
                For Each at In it.Attachments
                    at.SaveAsFile (Range("E3")) & "\" & at.DisplayName        'Range("E3") is the path
                Next
            End If
        Next
    Next

End Sub

最佳答案

首先,不要使用at.DisplayName - 使用 at.FileName .
其次,保存前检查附件类型——OOM不会让你保存附件,例如嵌入的OLE对象,你需要先检查附件类型。

For Each at In it.Attachments
  if at.Type = 1 Then 'olByValue
    at.SaveAsFile (Range("E3")) & "\" & at.FileName 'Range("E3") is the path
  End If
Next

关于excel - 根据主题使用vba下载outlook附件在其他计算机上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71238288/

相关文章:

rest - 有没有办法通过 Outlook API 获取建议的联系人?

excel - 使用工作表格式将 % 符号添加到列

excel - VBA : Me. 名称函数

VBA excel行复制方法不起作用

excel - 如何将包含公式的文本部分加粗?

php - 使用 php 读取 .pst 文件的内容

vba - 当 A 列中的项目的值在 B 列中至少找到一次时,返回一些东西

excel - 使用 double 的 VBA 溢出不正确 (Excel)

excel - 是否有一个公式可以在 Excel 中以二进制方式递增 1,然后重复该模式?

outlook - 名称 'application' 在当前上下文中不存在