简体   繁体   English

我可以使用文件中的内容作为名称在 Outlook 中自动保存 Excel 附件吗?

[英]Can I automatically save an excel attachment in Outlook using content from the file as the name?

I receive emails each day which give me a report of my site's performance for the previous day.我每天都会收到电子邮件,其中会向我提供有关前一天网站性能的报告。 The reports are given a generic name and I am not able to change this at source.这些报告被赋予了一个通用名称,我无法从源头上更改它。 I run the below script via an Outlook rule for whenever a message with certain criteria is received and the report is saved to a given location with yesterday's date in the file name:我通过 Outlook 规则运行以下脚本,每当收到具有特定条件的消息并将报告保存到文件名中包含昨天日期的给定位置时:

    Public Sub Save Reports (itm As Outlook.MailItem)

    Dim ObjAtt As Outlook.Attachment
    Dim SaveFolder As String

    For Each ObjAtt In itm.Attachments
        If InStr(ObjAtt.DisplayName, ".csv") Then


    FileName = (ObjAtt.FileName)
    NewName = "System Performance " & Format(Date - 1, "DD-MM-YYYY") & Right(FileName, 4)
    SaveFolder = "C:\Users\Me\Documents\"

    ObjAtt.SaveAsFile SaveFolder & NewName

    End If

    Set ObjAtt = Nothing

    Next
    End Sub

The problem is that if we have any problems anywhere within the process, I might get an email today which actually relates to last week rather than yesterday.问题是,如果我们在流程中的任何地方遇到任何问题,我今天可能会收到一封电子邮件,它实际上与上周而不是昨天有关。 If this happens the above script does not work and it requires me to save it manually.如果发生这种情况,上述脚本将不起作用,需要我手动保存。

One way I could work round this is if I can work out a way to extract data from a cell in the attached CSV file I am saving and then use that as the file name.我可以解决这个问题的一种方法是,如果我能找到一种方法从我正在保存的附加 CSV 文件中的单元格中提取数据,然后将其用作文件名。 For every file I want to save, cell B1 has the date that I need to use in the file name.对于我想保存的每个文件,单元格 B1 都有我需要在文件名中使用的日期。

I have look through Stackoverflow and other internet resources to try and find something that will allow me to do this but have been unable to work it out.我查看了 Stackoverflow 和其他互联网资源,试图找到一些可以让我做到这一点但一直无法解决的问题。

Thanks to a comment below I have tried to edit my script so saves the files, then opens the files and takes the data needed and then renames the file but to no avail:感谢下面的评论,我尝试编辑我的脚本以保存文件,然后打开文件并获取所需的数据,然后重命名文件但无济于事:

    Public Sub Save Reports (itm As Outlook.MailItem)

    Dim ObjAtt As Outlook.Attachment
    Dim SaveFolder As String
    Dim xlApp As Object
    Dim sourceWB As Excel.Workbook
    Dim sourceSH As Excel.Worksheet
    Dim strFile As String

    For Each ObjAtt In itm.Attachments
        If InStr(ObjAtt.DisplayName, ".csv") Then


    FileName = (ObjAtt.FileName)
    NewName = "System Performance " & Format(Date - 1, "DD-MM-YYYY") & Right(FileName, 4)
    SaveFolder = "C:\Users\Me\Documents\"

    ObjAtt.SaveAsFile SaveFolder & NewName

    Set xlApp = CreateObject("Excel.Application")
        With xlApp
            .Visible = True
            .EnableEvents = False
        End With

        strFile = SaveFolder & NewName

        Set sourceWB = Workbooks.Open(strFile, , False, , , , , , , True)
        Set sourceSH = sourceWB.Worksheets("Sheet2")
        sourceWB.Activate
    Range("B1").Select
    newdate = ActiveCell.Value
    Set sourceWB = Nothing
    Set sourceSH = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    Name SaveFolder & NewName As SaveFolder & newdate

    End If

    Set ObjAtt = Nothing

    Next
    End Sub

The Outlook object model doesn't provide any property or method for that. Outlook 对象模型不为此提供任何属性或方法。 You need to save the attached file on the disk first and then open it for reading the cells.您需要先将附加文件保存在磁盘上,然后打开它以读取单元格。 The SaveAsFile method of the Attachment class saves the attachment to the specified path. Attachment 类的SaveAsFile方法将附件保存到指定路径。

Also you can try to read the binary content of the attached file using the low-level API - Extended MAPI.您也可以尝试使用低级 API - 扩展 MAPI 读取附件的二进制内容。 The property name is PR_ATTACH_DATA_BIN which contains binary attachment data typically accessed through the OLE IStream interface.属性名称是 PR_ATTACH_DATA_BIN,它包含通常通过 OLE IStream 接口访问的二进制附件数据。 See Opening an Attachment for more information.有关详细信息,请参阅 打开附件 Also you may consider using any third-party wrappers around that API (for example, Redemption).此外,您可以考虑使用该 API 周围的任何第三方包装器(例如,Redemption)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM