热搜词
发表于 2008-7-18 17:41:48 | 显示全部楼层 |阅读模式
前几天有人问MS Office Outlook(不是outlook express)中自动批量保存附件的软件,今天翻了翻office vba编程的书,学习一下。写了一个非常简单的宏,没有任何出错处理,只能在找不到合适的软件时对付着用。在outlook 2002中测试通过。

outlook中alt+F8,输入宏的名字,创建,把以下代码拷贝进去。选择要保存附件的邮件(选中邮件中某封信没有附件也没关系,宏可以判断的),执行宏即可。附件保存到的目录写在了宏里,你自己改folder那个变量。保存后的附件名字后半部分是附件原始名字,前面加了“mail<顺序号>”的前缀,防止重名,想整理文件时分类也方便一些。
  1. Public Sub SaveAtt()
  2. Dim msg As MailItem
  3. Dim exp As Explorer
  4. Dim att As Attachment
  5. Dim mailIndex As Integer
  6. Dim path As String
  7. Dim folder As String

  8. Set exp = Application.ActiveExplorer

  9. '保存附件到哪个文件夹,末尾必须是斜杠
  10. folder = "c:\temp"
  11. mailIndex = 0

  12. For Each msg In exp.Selection

  13. If msg.Attachments.Count > 0 Then
  14. mailIndex = mailIndex + 1
  15. For Each att In msg.Attachments
  16. '所有附件保存到folder指定的文件夹中,文件命名为:mailatt<编号>_附件原始文件名
  17. path = folder + "mailatt" + CStr(mailIndex) + "_" + att.FileName
  18. att.SaveAsFile path
  19. Next
  20. End If

  21. Next

  22. End Sub
复制代码
改写了一下,可以按邮件地址分类创建目录来保存。
  1. Sub 批量保存附件()
  2. Dim msg As MailItem
  3. Dim exp As Explorer
  4. Dim att As Attachment
  5. Dim mailIndex As Integer
  6. Dim path As String
  7. Dim folder As String
  8. Dim info As String

  9. Set exp = Application.ActiveExplorer
  10. Set Fso = CreateObject("Scripting.FileSystemObject")

  11. '保存附件到哪个文件夹,末尾必须是斜杠
  12. folder = "D:\Office\att"
  13. mailIndex = 0

  14. For Each msg In exp.Selection
  15. If msg.Attachments.Count > 0 Then
  16. mailIndex = mailIndex + 1
  17. If Fso.FolderExists(folder + msg.SenderEmailAddress) = False Then
  18. MkDir (folder + msg.SenderEmailAddress)
  19. End If

  20. For Each att In msg.Attachments
  21. '所有附件保存到folder指定的文件夹中,文件命名为:mailatt<编号>_附件原始文件名
  22. path = folder + CStr(msg.SenderEmailAddress) + "" + CStr(mailIndex) + "_" + att.FileName
  23. att.SaveAsFile path
  24. info = info & Chr(10) & "主题:" + msg.Subject & Chr(10) & "附件数量:" + CStr(msg.Attachments.Count)
  25. Next
  26. End If
  27. Next
  28. MsgBox info, vbInformation, "保存附件"
  29. End Sub
复制代码

[ 本帖最后由 灰儿 于 2008-7-18 17:56 编辑 ]
全部评论1
灰儿 发表于 2008-7-18 18:06:49 | 显示全部楼层
  1. Sub 批量保存附件()
  2. Dim msg As MailItem
  3. Dim exp As Explorer
  4. Dim att As Attachment
  5. Dim mailIndex As Integer
  6. Dim path As String
  7. Dim folder As String
  8. Dim info As String

  9. Set exp = Application.ActiveExplorer
  10. Set Fso = CreateObject("Scripting.FileSystemObject")

  11. '保存附件到哪个文件夹,末尾必须是斜杠
  12. folder = "D:\Office"
  13. mailIndex = 0

  14. For Each msg In exp.Selection
  15. If msg.Attachments.Count > 0 Then
  16. mailIndex = mailIndex + 1
  17. For Each att In msg.Attachments
  18. '所有附件保存到folder指定的文件夹中,文件命名为:mailatt<编号>_附件原始文件名
  19. path = folder + CStr(mailIndex) + "_" + att.FileName
  20. att.SaveAsFile path
  21. info = info & Chr(10) & "主题:" + msg.Subject & Chr(10) & "附件数量:" + CStr(msg.Attachments.Count)
  22. Next
  23. End If
  24. Next
  25. MsgBox info, vbInformation, "保存附件"
  26. End Sub
复制代码
回复

使用道具 举报

回复
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|管理员之家 ( 苏ICP备2023053177号-2 )

GMT+8, 2024-11-25 06:52 , Processed in 0.151841 second(s), 22 queries .

Powered by Discuz! X3.5

Cpoyright © 2001-2024 Discuz! Team