【问题标题】:How to open an Outlook excel attachment using Excel VBA, sent in a particular time range to a specific Outlook folder?如何使用 Excel VBA 打开 Outlook excel 附件,在特定时间范围内发送到特定 Outlook 文件夹?
【发布时间】:2019-10-06 05:59:03
【问题描述】:

这是我的第一个问题。我想主修计算机科学,但与此同时,我恳请您在使用 VBA Excel 时提供一些帮助。

我每天(工作日)都会收到具有相同文件名的 Outlook 电子邮件;我想对 VBA Excel 进行编程以打开前一天晚上收到的 Outlook 附件。

所以,真的有两件事:

  1. 编写 VBA Excel 以打开特定文件夹中的附件
  2. 删除同一封电子邮件,以便第二天,我有相同的过程(所有文件都使用相同的文件名发送,所以我不想混淆自己或复制粘贴错误的日期数据)。

我已经编写了以下代码,但需要从 Outlook 打开 Excel 工作簿,而不是 C 盘:

Sub Test()

    Dim WB As Workbook      'designate variables
    Dim sSaveFileName As String


    sSaveFileName = Format(Now() - 1, "MM-DD-YYYY")  'save as received date, 
    yesterday

    ' not needed >>>>> 'Set WB = 
    Workbooks.Open("C:\Users\nader\OneDrive\Documents\openthisexcel.xlsx")


    Windows("openthisexcel.xlsx").Activate  'activate opened excel attachment
    Sheets("Sheet1").Select
    Range("A1:A50").Select
    Selection.Copy                          'select range and copy

    Windows("macroExcel.xlsm").Activate    'activate xlsm template (another 
    workbook)
    Sheets("Sheet1").Select
    Range("A1:A50").Select
    Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False                          'select range 
    and paste from one excel to another

    WB.SaveAs Filename:= _
        "C:\Users\nader\OneDrive\Documents\openthisexcel.xlsx" & 
    sSaveFileName & ".xlsx"        'save received excel using filename and 
    the date it was received in Outlook


    WB.Close  'close the excel attachment


End Sub

请提供一些帮助以从文件夹中打开 Excel 附件以及如何使用 VBA 在 Outlook 中删除电子邮件(包括 Excel 附件)。

我阅读了一些 Stack Overflow 帖子,但无法理解(这里只是初学者)。

【问题讨论】:

  • 如果我理解正确,您每天都会收到附加到 Outlook 电子邮件的 Excel 文件中的数据,并且您希望自动将该数据复制到其他已知的 Excel 文件中。是这样吗?
  • 没错,罗伯特!我每天都从具有相同文件名的报告中收到数据;只想将该数据复制到新工作簿中;另存为文件名和昨天的日期(或接收日期);该代码有效,但只想连接到 Outlook 文件夹以打开文件(其他一切都应该很好)。哦,我还想在完成后从文件夹中删除电子邮件,以便第二天重新开始

标签: excel vba outlook


【解决方案1】:

看看Download attachment from Outlook and Open in Excel。这可以满足您对 Excel 的要求。

但是,您还有另一种选择,即改为编写 Outlook 宏。见Running Outlook Macros on a Schedule。这样,您就可以每天在 Outlook 中自动运行宏(只要打开 Outlook)来提取数据。宏的内容与第一个链接中的内容基本相同。

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2017-09-22
    相关资源
    最近更新 更多