【问题标题】:Dwonload attachment from specific sender and open in excel从特定发件人处下载附件并在 Excel 中打开
【发布时间】:2016-06-14 21:33:38
【问题描述】:

我对 VBA 比较陌生,希望能在某个项目上获得一些帮助。为了给你一些背景知识,我大约每 15 分钟在 Outlook 中收到一封带有 Excel 附件的电子邮件。收到电子邮件后,我需要打开附件并查看它/将其与 15 分钟前发送的电子邮件进行比较。如果电子邮件有差异,那么我必须执行操作。我希望至少自动化其中的一些过程。理想情况下,我可以使用宏来扫描我的收件箱以查找来自特定发件人的任何新邮件。如果它找到一条消息,它可以检查附件,如果附件在那里,它会下载并打开它。

在理想情况下,我可以做的另一件事是将之前的 excel 附件与当前附件进行比较,如果不同,则 ping 一条消息(警报)。

任何帮助将不胜感激。正如我所说,我是 VBA 新手,但我正在尽力理解函数。

【问题讨论】:

  • 欢迎来到 StackOverflow。请注意,这不是免费的代码编写服务。然而,我们渴望用他们的代码帮助其他程序员(和有志者)。请阅读How do I Ask a Good Question 上的帮助主题。您可能还想take the tour 并在这样做的同时获得徽章。之后,请使用您迄今为止编写的 VBA 代码更新您的问题,以完成您希望完成的任务。

标签: vba excel email outlook


【解决方案1】:

这应该可以帮助您入门。假设您已在 Outlook 中选择了电子邮件:

Sub check_for_changes()
    'Created by Fredrik Östman www.scoc.se
    Dim myOlApp As New Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
    Set myOlExp = myOlApp.Explorers.Item(1)
    Set myOlSel = myOlExp.Selection
    Set mymail = myOlSel.Item(1)
    Dim myAttachments As Outlook.Attachments
    Set myAttachments = mymail.Attachments
    Dim Atmt As Attachment
    Set Atmt = myAttachments(1)

    new_file_name = "C:\tmp\new_received_file.xlsx"
    old_file_name = "C:\tmp\old_received_file.xlsx"

    FileCopy new_file_name, old_file_name

    Atmt.SaveAsFile new_file_name

    Dim eApp As Object
    Set eApp = CreateObject("Excel.Application")

    eApp.Application.Visible = True

    Dim new_file As Object
    eApp.workbooks.Open new_file_name
    Set new_file = eApp.ActiveWorkbook

    Dim old_file As Object
    eApp.workbooks.Open old_file_name
    Set old_file = eApp.ActiveWorkbook

    'Find range to compare
    start_row = old_file.sheets(1).usedrange.Row
    If new_file.sheets(1).usedrange.Row > start_row Then start_row = new_file.sheets(1).usedrange.Row

    end_row = old_file.sheets(1).usedrange.Row + old_file.sheets(1).usedrange.Rows.Count
    If new_file.sheets(1).usedrange.Rows.Count + new_file.sheets(1).usedrange.Row > end_row Then end_row = new_file.sheets(1).usedrange.Rows.Count + new_file.sheets(1).usedrange.Row

    start_col = old_file.sheets(1).usedrange.Column
    If new_file.sheets(1).usedrange.Column > start_col Then start_col = new_file.sheets(1).usedrange.Column

    end_col = old_file.sheets(1).usedrange.Column + old_file.sheets(1).usedrange.Columns.Count
    If new_file.sheets(1).usedrange.Columns.Count + new_file.sheets(1).usedrange.Column > end_row Then end_row = new_file.sheets(1).usedrange.Columns.Count + new_file.sheets(1).usedrange.Column

    'Check all cells
    something_changed = False
    For i = start_row To end_row
        For j = start_col To end_col
            If new_file.sheets(1).Cells(i, j) <> old_file.sheets(1).Cells(i, j) Then
                new_file.sheets(1).Cells(i, j).Interior.ColorIndex = 3 'Mark red
                something_changed = True
            End If
        Next j
    Next i

    If something_changed Then
        new_file.Activate
    Else
        new_file.Close
        old_file.Close
        If eApp.workbooks.Count = 0 Then eApp.Quit
        MsgBox "No changes"
    End If

End Sub

【讨论】:

  • 顺便说一句,我假设只有一张纸(可以通过额外的循环修复)并且更改将在单元格值中而不是格式化。代码应该放在outlook中,然后当新邮件从人x到达主题y等时触发它。
【解决方案2】:

有趣的问题,我会带你从前景部分开始。您可能希望在 Outlook 和 Excel 之间拆分问题。

这是我用来保存在 Outlook 中发送的每个附件以节省空间的一些代码。

Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim pobjMsg As Outlook.MailItem 'Object
Dim objSelection As Outlook.Selection

On Error Resume Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

For Each pobjMsg In objSelection
    SaveAttachments_Parameter pobjMsg
Next

ExitSub:
Set pobjMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
MsgBox "Export Complete"
End Sub
Public Sub SaveAttachments_Parameter(objMsg As MailItem)
Dim objAttachments As Outlook.Attachments
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String

' Get the path to your My Documents folder
strFolderpath = "C:\Users\******\Documents\Reports\"
'On Error Resume Next
' Set the Attachment folder.
strFolderpath = strFolderpath & "Outlook Attachments\"
' Get the Attachments collection of the item.
Set objAttachments = objMsg.Attachments
lngCount = objAttachments.Count

If lngCount > 0 Then
' We need to use a count down loop for removing items' from a collection. Otherwise, the loop counter gets' confused and only every other item is removed.
    For i = lngCount To 1 Step -1
        ' Save attachment before deleting from item.
        ' Get the file name.
        strFile = objAttachments.Item(i).FileName
        If Right(strFile, 4) = ".png" Or Right(strFile, 4) = ".jpg" Or Right(strFile, 4) = ".gif" Then
        GoTo cont
        End If
        ' Combine with the path to the Temp folder.
        strFile = strFolderpath & objMsg.SenderName & "." & Format(objMsg.ReceivedTime, "yyyy-MM-dd h-mm-ss") & "." & strFile
        ' Save the attachment as a file.
        objAttachments.Item(i).SaveAsFile strFile

        ' Delete the attachment - You might not want this part
        'objAttachments.Item(i).Delete

        'write the save as path to a string to add to the message
        'check for html and use html tags in link
        If objMsg.BodyFormat = olFormatHTML Then
            strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & Replace(strFile, " ", "%20") & ">"
        Else
            strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & Replace(strFile, " ", "%20") & ">"
        End If
cont:
    Next i

    ' Adds the filename string to the message body and save it
    ' Check for HTML body
    If objMsg.BodyFormat = olFormatHTML Then
        objMsg.Body = "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.Body
    Else
        objMsg.HTMLBody = "The file(s) were saved to " & strDeletedFiles & vbCrLf & objMsg.HTMLBody
    End If

    objMsg.Save
End If


ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objOL = Nothing
End Sub

代码中说的部分

    If Right(strFile, 4) = ".png" Or Right(strFile, 4) = ".jpg" Or Right(strFile, 4) = ".gif" Then
    GoTo cont

你可以改成这样的:

    If objMsg.SenderName = "John Smith" Then
    GoTo cont

这样它只会保存来自该特定发件人的附件。

然后,一旦您有两个或多个文件,您可以在 excel 中使用另一个宏加载文件并比较这两个文件,如果有任何差异,请发送电子邮件给您。

希望能帮助您入门。

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多