在 Outlook 中获取/打开邮件附件

发布于 2024-11-02 08:43:44 字数 67 浏览 1 评论 0原文

我正在尝试找到一种方法来获取邮件附件(以编程方式),然后打开它...有人可以告诉我如何执行此操作或为我指出正确的方向吗?

I'm trying to find a way to get mail attachments (programaticaly) , and then open it... can someone tell me how to do this or point me to a right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-11-09 08:43:44

这是一个 VBA 脚本(在 Outlook 中的宏中使用),它将循环遍历当前文件夹中所有选定项目中的所有附件并将它们保存到磁盘。

它应该可以帮助您开始,并且我认为添加某种流程启动而不是向其保存逻辑并不需要太多。

 Public Sub SaveAttachments()

  'Note, this assumes you are in the a folder with e-mail messages when you run it.
  'It does not have to be the inbox, simply any folder with e-mail messages

  Dim Exp As Outlook.Explorer
  Dim Sel As Outlook.Selection

  Dim AttachmentCnt As Integer
  Dim AttTotal As Integer
  Dim MsgTotal As Integer

  Dim outputDir As String
  Dim outputFile As String
  Dim fileExists As Boolean
  Dim cnt As Integer

  'Requires reference to Microsoft Scripting Runtime (SCRRUN.DLL)
  Dim fso As FileSystemObject

  Set Exp = Application.ActiveExplorer

  Set Sel = Exp.Selection
  Set fso = New FileSystemObject

  outputDir = "C:\Path"
  If outputDir = "" Then
    MsgBox "You must pick an directory to save your files to. Exiting SaveAttachments.", vbCritical, "SaveAttachments"
    Exit Sub
  End If

   Dim att As Attachment

  'Loop thru each selected item in the inbox
  For cnt = 1 To Sel.Count
    'If the e-mail has attachments...
    If Sel.Item(cnt).Attachments.Count > 0 Then
      MsgTotal = MsgTotal + 1

      'For each attachment on the message...
      For AttachmentCnt = 1 To Sel.Item(cnt).Attachments.Count
        'Get the attachment

        Set att = Sel.Item(cnt).Attachments.Item(AttachmentCnt)
        outputFile = att.FileName

        outputFile = Format(Sel.Item(cnt).ReceivedTime, "yyyy-mm-dd_hhmmss - ") + outputFile

        fileExists = fso.fileExists(outputDir + outputFile)

        'Save it to disk if the file does not exist
        If fileExists = False Then
          att.SaveAsFile (outputDir + outputFile)
          AttTotal = AttTotal + 1
        End If

        Set att = Nothing

        Sel.Item(cnt).Close (Outlook.OlInspectorClose.olDiscard)

      Next
    End If
  Next

  'Clean up
  Set Sel = Nothing
  Set Exp = Nothing
  Set fso = Nothing

  'Let user know we are done
  Dim doneMsg As String
  doneMsg = "Completed saving " + Format$(AttTotal, "#,0") + " attachments in " + Format$(MsgTotal, "#,0") + " Messages."
  MsgBox doneMsg, vbOKOnly, "Save Attachments"

  Exit Sub

ErrorHandler:

  Dim errMsg As String
  errMsg = "An error has occurred. Error " + Err.Number + " " + Err.Description
  Dim errResult As VbMsgBoxResult
  errResult = MsgBox(errMsg, vbAbortRetryIgnore, "Error in Save Attachments")
  Select Case errResult
    Case vbAbort
      Exit Sub

    Case vbRetry
      Resume

    Case vbIgnore
      Resume Next

  End Select

End Sub

This is a VBA script (use in a Macro in Outlook) which will loop over all attachments in all selected items in the current folder and Save them to disk.

It should get you started and I don't think it would take much to add some kind of process launch rather than save logic to it.

 Public Sub SaveAttachments()

  'Note, this assumes you are in the a folder with e-mail messages when you run it.
  'It does not have to be the inbox, simply any folder with e-mail messages

  Dim Exp As Outlook.Explorer
  Dim Sel As Outlook.Selection

  Dim AttachmentCnt As Integer
  Dim AttTotal As Integer
  Dim MsgTotal As Integer

  Dim outputDir As String
  Dim outputFile As String
  Dim fileExists As Boolean
  Dim cnt As Integer

  'Requires reference to Microsoft Scripting Runtime (SCRRUN.DLL)
  Dim fso As FileSystemObject

  Set Exp = Application.ActiveExplorer

  Set Sel = Exp.Selection
  Set fso = New FileSystemObject

  outputDir = "C:\Path"
  If outputDir = "" Then
    MsgBox "You must pick an directory to save your files to. Exiting SaveAttachments.", vbCritical, "SaveAttachments"
    Exit Sub
  End If

   Dim att As Attachment

  'Loop thru each selected item in the inbox
  For cnt = 1 To Sel.Count
    'If the e-mail has attachments...
    If Sel.Item(cnt).Attachments.Count > 0 Then
      MsgTotal = MsgTotal + 1

      'For each attachment on the message...
      For AttachmentCnt = 1 To Sel.Item(cnt).Attachments.Count
        'Get the attachment

        Set att = Sel.Item(cnt).Attachments.Item(AttachmentCnt)
        outputFile = att.FileName

        outputFile = Format(Sel.Item(cnt).ReceivedTime, "yyyy-mm-dd_hhmmss - ") + outputFile

        fileExists = fso.fileExists(outputDir + outputFile)

        'Save it to disk if the file does not exist
        If fileExists = False Then
          att.SaveAsFile (outputDir + outputFile)
          AttTotal = AttTotal + 1
        End If

        Set att = Nothing

        Sel.Item(cnt).Close (Outlook.OlInspectorClose.olDiscard)

      Next
    End If
  Next

  'Clean up
  Set Sel = Nothing
  Set Exp = Nothing
  Set fso = Nothing

  'Let user know we are done
  Dim doneMsg As String
  doneMsg = "Completed saving " + Format$(AttTotal, "#,0") + " attachments in " + Format$(MsgTotal, "#,0") + " Messages."
  MsgBox doneMsg, vbOKOnly, "Save Attachments"

  Exit Sub

ErrorHandler:

  Dim errMsg As String
  errMsg = "An error has occurred. Error " + Err.Number + " " + Err.Description
  Dim errResult As VbMsgBoxResult
  errResult = MsgBox(errMsg, vbAbortRetryIgnore, "Error in Save Attachments")
  Select Case errResult
    Case vbAbort
      Exit Sub

    Case vbRetry
      Resume

    Case vbIgnore
      Resume Next

  End Select

End Sub
没︽人懂的悲伤 2024-11-09 08:43:44

查找 Outlook 的 COM 引用。
您还可以使用用 vba 编写的宏来执行此操作。

Look for the COM-reference of Outlook.
You can also use a macro written in vba to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文