【问题标题】:Download email attachment to a specific folder then open an excel file using VBA in outlook将电子邮件附件下载到特定文件夹,然后在 Outlook 中使用 VBA 打开一个 excel 文件
【发布时间】:2021-08-24 11:54:29
【问题描述】:

我已将 Outlook 设置为在收到来自特定电子邮件地址的电子邮件时运行以下脚本。我希望脚本下载附件然后打开一个 excel 文件,但出现以下错误:Compile error: User-defined type not defined。我不确定我做错了什么,有人可以帮忙。下面是代码:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim source As Excel.Workbook
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = New Excel.Application

saveFolder = "C:\Reports\Daily Traffic Report per Site\Source"
        
            On Error Resume Next
Kill "C:\Reports\Source\*.*"
On Error GoTo 0

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
    Next
    
    Set ExWbk = ExApp.Workbooks.Open("C:\Reports\Calculation.xlsm")
    
End Sub

【问题讨论】:

  • 您是否在 Outlook VBA 项目中包含了对 excel 对象库的引用?

标签: excel vba outlook


【解决方案1】:

要使用早期绑定,您首先需要引用可用的 Outlook 对象库。要从 Visual Basic (VB) 或 Visual Basic for Applications 执行此操作,如果您尝试在 Outlook 中运行代码,请按照以下步骤操作:

  1. 在 Visual Basic 编辑器的 Tools 菜单上,单击 References
  2. 单击以选中Microsoft Excel XX.0 Object Library 复选框,然后单击“确定”。

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2017-10-23
    • 2016-08-03
    • 2019-03-18
    • 2022-11-24
    • 2016-02-05
    • 2019-12-24
    • 1970-01-01
    • 2021-10-28
    相关资源
    最近更新 更多