excel - 使用 VBA 从 Outlook 2010 保存 .XLSX 附件

标签 excel outlook vba

我们使用 Outlook 2010 并接收带有 Excel 附件的电子邮件。我们手动将附件保存在我们在网络驱动器上的分区文件夹中创建的子文件夹中。

我很好奇的是是否有可能

  1. 使用代码检查传入的电子邮件,看看它们是否有附件,
  2. 然后检查附件是否为 .XLSX,
  3. 如果是,请打开附件,检查特定单元格的值,
  4. 然后将帐户名和帐号存储为字符串和变量
  5. 然后使用它们在相应的 Windows 目录中创建子文件夹。

** 我忘记发布到目前为止我所做的事情。我相信布雷特回答了我的??,但也许其他人可以使用它的片段。

Private Sub cmdConnectToOutlook_Click()
Dim appOutlook As Outlook.Application
Dim ns As Outlook.Namespace
Dim inbox As Outlook.MAPIFolder
Dim item As Object
Dim atmt As Outlook.Attachment
Dim filename As String
Dim i As Integer

Set appOutlook = GetObject(, "Outlook.Application")
Set ns = appOutlook.GetNamespace("MAPI")
Set inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0 

If inbox.Items.Count = 0 Then
    MsgBox "There are no messages in the Inbox.", vbInformation, _
           "Nothing Found"
    Exit Sub
End If

For Each item In inbox.Items
  For Each atmt In item.Attachments

    If Right(atmt.filename, 4) = "xlsx" Then
        filename = "\\temp\" & atmt.filename
        atmt.SaveAsFile filename
       i = i + 1
    End If

  Next atmt
Next item

MsgBox "Attachments have been saved.", vbInformation, "Finished"

Set atmt = Nothing
Set item = Nothing
Set ns = Nothing

结束子

最佳答案

虽然这里很长,但这是一种方法。我的代码来自VBA Code to save an attachment (excel file) from an Outlook email that was inside another email as an attachment可能也会感兴趣

您将需要更新文件路径以及正在打开的文件的单元格范围

在我的测试中,我向自己发送了一条消息,其中包含一个 pdf 文件和一个 Excel 工作簿,第一张表的 A1 中包含“bob”

下面的代码找到 Excel 文件,保存它,打开它,创建一个目录 c:\temp\bob 然后杀死保存的文件

Private Sub Application_NewMailEx _
    (ByVal EntryIDCollection As String)

'Uses the new mail techniquer from http://www.outlookcode.com/article.aspx?id=62

Dim arr() As String
Dim lngCnt As Long
Dim olAtt As Attachment
Dim strFolder As String
Dim strFileName As String
Dim strNewFolder
Dim olns As Outlook.NameSpace
Dim olItem As MailItem
Dim objExcel As Object
Dim objWB As Object

'Open Excel in the background
Set objExcel = CreateObject("excel.application")

'Set working folder
strFolder = "c:\temp"

On Error Resume Next
Set olns = Application.Session
arr = Split(EntryIDCollection, ",")
On Error GoTo 0

For lngCnt = 0 To UBound(arr)
    Set olItem = olns.GetItemFromID(arr(lngCnt))
    'Check new item is a mail message
    If olItem.Class = olMail Then
        'Force code to count attachments
        DoEvents
        For Each olAtt In olItem.Attachments
            'Check attachments have at least 5 characters before matching a ".xlsx" string
            If Len(olAtt.FileName) >= 5 Then
                If Right$(olAtt.FileName, 5) = ".xlsx" Then
                    strFileName = strFolder & "\" & olAtt.FileName
                    'Save xl attachemnt to working folder
                    olAtt.SaveAsFile strFileName
                    On Error Resume Next
                    'Open excel workbook and make a sub directory in the working folder with the value from A1 of the first sheet
                    Set objWB = objExcel.Workbooks.Open(strFileName)
                    MkDir strFolder & "\" & objWB.sheets(1).Range("A1")
                    'Close the xl file
                    objWB.Close False
                    'Delete the saved attachment
                    Kill strFileName
                    On Error Goto 0
                End If
            End If
        Next
    End If
Next
'tidy up
Set olns = Nothing
Set olItem = Nothing
objExcel.Quit
Set objExcel = Nothing
End Sub

关于excel - 使用 VBA 从 Outlook 2010 保存 .XLSX 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439543/

相关文章:

excel - 如何让我的 Excel 公式在以编程方式添加值后进行计算

c# - 如何使用 OleDbDataAdapter 只读取 Excel 文件的第一行?

excel文件未保存到宏指定的文件路径

javascript - 如何在浏览器的 mailto href 中包含德语字符?

c# - 在 C# 中通过 MailMessage 取消 outlook session 请求

html - 最小宽度嵌套表格 HTML 电子邮件

vba - 在 Excel 中将批量文本转换为超链接

vba - IF FIND 函数在 vba 中找不到任何内容

excel - Excel 条件格式错误百分比

vba - ADO 记录集未在 VBA 中保持打开状态 - "Operation is not allowed when the object is closed"