ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 13956|回复: 9

[讨论] 关于OUTLOOK附件加密码及密码通知邮件的发送

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-11-20 10:19 | 显示全部楼层 |阅读模式
本帖最后由 zfengyue 于 2012-11-20 10:18 编辑

老板要求邮件附件要加密.之前用一个软件手动加密成EXE文件,很繁琐,收件人解密也很麻烦.

现在我想用OUTLOOKVBA来自动完成这个过程.加密写好了.
但是密码通知邮件有些问题.
不知道怎么写.斑竹可不可以给些提示.
我把我已经写好的放上.
应该写得比较乱,初学VBA,大家包涵下.

  1. Public PW_Mail_Addresses As String
  2. Public PW_Subject As String
  3. Public My_PW As String

  4. Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
  5. 'On Error GoTo Err

  6. 'if  folders does not exist,then create them.

  7. SaveFolder = "C:\temp\outlook_attachments"
  8. Attachments_log = "C:\temp\log"
  9. ZipDir = "C:\temp\ZipDir"

  10. If Dir(SaveFolder, vbDirectory) = Empty Then MkDir (SaveFolder)
  11. If Dir(ZipDir, vbDirectory) = Empty Then MkDir (ZipDir)
  12. If Dir(Attachments_log, vbDirectory) = Empty Then MkDir (Attachments_log)

  13. If Item.Attachments.Count <> 0 Then
  14.   Dim ZipFilename As String
  15.     Select Case Item.Attachments.Count
  16.      Case 1
  17.      ZipFilename = Replace(Item.Attachments.Item(1).DisplayName, " ", "_") & ".zip"
  18.      Case Else
  19.      ZipFilename = "Attachments.zip"
  20.     End Select
  21.   
  22.   'Save original attachments to $SaveFolder.
  23.     For i = 1 To Item.Attachments.Count
  24.      Item.Attachments.Item(i).SaveAsFile SaveFolder & Item.Attachments.Item(i).DisplayName
  25.     Next
  26.    
  27.   'Remove original attachments.
  28.   Do While Item.Attachments.Count > 0
  29.   Item.Attachments.Remove 1
  30.   Loop
  31. End If


  32. DirZipFilename = ZipDir & ZipFilename
  33. 'msgbox ZipFilename
  34. My_PW = GeneratePW()
  35. MyStr = "C:\Program Files\7-Zip\7z a" & " " & DirZipFilename & " -p" & My_PW & " " & SaveFolder & "*"
  36. 'msgbox MyStr
  37. Shell MyStr

  38. 'Wait until zip file is created.

  39. Do Until Dir(DirZipFilename, vbNormal) <> vbNullString
  40. WaitASec
  41. Loop

  42. Item.Attachments.Add (DirZipFilename)

  43. 'write log
  44. My_Log = Now() & vbTab & GetDirFiles(SaveFolder) & vbTab & GetDirFiles(ZipDir) & vbTab & My_PW
  45. Open "c:\temp\log\Outlook.Attachments.log.txt" For Append As #1
  46. Write #1, My_Log
  47. Close #1

  48. 'delete temp files
  49. DelAttachments = "cmd.exe /c del /Q " & SaveFolder & "*"
  50. DelZipFile = "cmd.exe /c del  /Q " & ZipDir & "*"
  51. Shell DelAttachments
  52. Shell DelZipFile

  53. Item.Display
  54. Cancel = True

  55. PW_Subject = Item.Subject & "(Password Notification Mail)"
  56. PW_Mail_Addresses = ""
  57. For i = 1 To Item.Recipients.Count
  58. PW_Mail_Addresses = Item.Recipients.Item(i).Address & ";" & PW_Mail_Addresses
  59. Next


  60. Call CreatePWMail(PW_Mail_Addresses, My_PW, PW_Subject) '这个地方会报错

  61. Exit Sub

  62. Err:
  63.   MsgBox "Error: Something is wrong."
  64.   Cancel = True
  65. End Sub


  66. Public Function GetDirFiles(MyGetDir)

  67. Set fs = CreateObject("Scripting.FileSystemObject")
  68. Set f = fs.GetFolder(MyGetDir)
  69. Set flist = f.Files

  70. For Each i In flist

  71. MyFileList = i & ", " & MyFileList

  72. Next

  73. GetDirFiles = MyFileList

  74. End Function

  75. Public Function GeneratePW()
  76. Dim str As String
  77.     Do Until Len(str) = 8
  78.     i = Int((75 * Rnd) + 48)
  79.     Select Case i
  80.     Case 48 To 57, 65 To 90, 97 To 122
  81.     str = str & Chr(i)
  82.     End Select
  83.     Loop
  84.    
  85.     GeneratePW = str
  86. End Function


  87. Public Function WaitASec()
  88. Savetime = Timer
  89. While Timer < Savetime + 1
  90. DoEvents
  91. Wend
  92. End Function


  93. Public Sub CreatePWMail(MailAddess As String, MyPassword As String, MySubject As String)

  94. Set objOL = CreateObject("Outlook.Application")
  95. Set itmNewMail = objOL.CreateItem(olMailItem)

  96. With itmNewMail
  97. .Subject = MySubject
  98. .BCC = MailAddess
  99. .Body = MyPassword
  100. .Display
  101. End With

  102. End Sub
复制代码



"WordMail不能启动" <- 报这个错误.

报错

报错

TA的精华主题

TA的得分主题

发表于 2012-11-20 14:39 | 显示全部楼层
你把这段代码放到什么地方了?

试了下,没报错啊。

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-11-21 08:51 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我放在THISOUTLOOKSESSION里。

TA的精华主题

TA的得分主题

发表于 2012-11-21 08:58 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-11-21 10:50 | 显示全部楼层
本帖最后由 zfengyue 于 2012-11-21 12:25 编辑
dsd999 发表于 2012-11-21 08:58
下图里的前两项都别选。

谢谢斑竹。按照你的指点,我再稍微修改了一下,现在已经可以向老板交差了

TA的精华主题

TA的得分主题

发表于 2012-11-21 13:27 | 显示全部楼层
zfengyue 发表于 2012-11-21 10:50
谢谢斑竹。按照你的指点,我再稍微修改了一下,现在已经可以向老板交差了

能否把最终代码贴上来。

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-11-28 08:23 | 显示全部楼层
本帖最后由 zfengyue 于 2012-11-28 08:34 编辑

不好意思,休了几天假,没有的来公司,今天才看到。

当然可以啦!
本来就是在EH的帮助下整出来的东西。
帮忙看一下,有些什么问题,好再改善下,好像基本上可以用了。

  1. Public PW_Mail_Addresses As String
  2. Public PW_Subject As String
  3. Public My_PW As String

  4. Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

  5. On Error GoTo Err

  6. If Item.Attachments.Count <> 0 Then
  7. If Right(Item.Attachments.Item(1).DisplayName, 13) = "Encrypted.zip" Then GoTo SendNow

  8. 'if folders does not exist,then create them.

  9. TEMP = "C:\temp"
  10. SaveFolder = "C:\temp\outlook_attachments"
  11. Attachments_log = "C:\temp\log"
  12. ZipDir = "C:\temp\ZipDir"

  13. If Dir(TEMP, vbDirectory) = Empty Then MkDir ("temp")
  14. If Dir(SaveFolder, vbDirectory) = Empty Then MkDir (SaveFolder)
  15. If Dir(ZipDir, vbDirectory) = Empty Then MkDir (ZipDir)
  16. If Dir(Attachments_log, vbDirectory) = Empty Then MkDir (Attachments_log)

  17. 'Clear the work dirs first.
  18. ClearFolder (SaveFolder)
  19. ClearFolder (ZipDir)

  20.     Dim ZipFilename As String
  21.     Select Case Item.Attachments.Count
  22.     Case 1
  23.     ZipFilename = Replace(Item.Attachments.Item(1).DisplayName, " ", "_") & "_Encrypted.zip"
  24.     Case Else
  25.     ZipFilename = "Attachments_Encrypted.zip"
  26.     End Select
  27.    
  28.      'Save original attachments to $SaveFolder.
  29.     For i = 1 To Item.Attachments.Count
  30.     Item.Attachments.Item(i).SaveAsFile SaveFolder & Item.Attachments.Item(i).DisplayName
  31.     Next
  32.    
  33.     'Remove original attachments.
  34.     Do While Item.Attachments.Count > 0
  35.     Item.Attachments.Remove 1
  36.     Loop

  37. DirZipFilename = ZipDir & ZipFilename
  38. My_PW = GeneratePW()

  39. Dim MYSTR As String
  40. MYSTR = "C:\Program Files\7-Zip\7z a" & " " & DirZipFilename & " -p" & My_PW & " " & SaveFolder & "*"

  41. CompressOutput = ShellAndWait(MYSTR)

  42. If Not InStr(1, CompressOutput, "Everything is Ok") <> 0 Then
  43. MsgBox "&#144;3&#143;í&#8218;é&#710;&#195;&#141;&#8224;‰&#187;&#8218;&#197;&#8218;&#171;&#8218;ü&#8218;1&#8218;&#241;&#8218;&#197;&#8218;μ&#8218;&#189;&#129;B", vbCritical
  44. End If

  45. TempBody = Item.HTMLBody
  46. HeadBody = "" & _
  47. "   –{&#402;&#129;&#129;[&#402;&#8249;&#8218;í“Y&#8226;t&#402;t&#402;@&#402;C&#402;&#8249;&#8218;ì&#710;&#195;&#141;&#8224;‰&#187;&#8218;a&#141;s&#8218;í&#8218;ê&#8218;&#196;&#8218;¢&#8218;ü&#8218;·&#129;B
  48. " & _
  49. "    &#338;&#227;&#8218;ù&#8218;&#199;‘—&#144;M&#381;ò&#8218;&#230;&#8218;è&#8226;&#161;&#141;&#8225;&#402;L&#129;[&#8218;a’ê’m&#8218;3&#8218;ê&#8218;ü&#8218;·&#129;B
  50. " & _
  51. "
  52. " & _
  53. "    " & ChrW(26412) & ChrW(37038) & ChrW(20214) & ChrW(30340) & ChrW(38468) & ChrW(20214) & ChrW(24050) & ChrW(21152) & ChrW(23494) & ChrW(65292) & ChrW(23494) & ChrW(30721) & ChrW(20250) & ChrW(22312) & ChrW(31245) & ChrW(21518) & ChrW(21457) & ChrW(36865) & ChrW(12290) & "
  54. " & _
  55. "   " & _
  56. "" & _
  57. "   
  58. " & _
  59. ""

  60. 'Wait until zip file is created.

  61. Item.Attachments.Add (DirZipFilename)

  62. Item.HTMLBody = HeadBody & TempBody

  63. 'write log
  64. My_Log = Now() & vbTab & GetDirFiles(SaveFolder) & vbTab & GetDirFiles(ZipDir) & vbTab & My_PW
  65. Open "c:\temp\log\Outlook.Attachments.log.txt" For Append As #1
  66. Write #1, My_Log
  67. Close #1

  68. 'delete temp files
  69. ClearFolder (SaveFolder)
  70. ClearFolder (ZipDir)

  71. Item.Display
  72. Cancel = True

  73. PW_Subject = Item.Subject & "(Password Notification E-mail)"
  74. PW_Mail_Addresses = ""
  75. For i = 1 To Item.Recipients.Count
  76. PW_Mail_Addresses = Item.Recipients.Item(i).Address & ";" & PW_Mail_Addresses
  77. Next

  78. Call CreatePWMail(PW_Mail_Addresses, My_PW, PW_Subject) '

  79. End If

  80. Item.Display 'Display the original email.
  81. Exit Sub

  82. Err:
  83.     MsgBox "Error: Something is wrong."
  84.     Cancel = True
  85.    
  86. SendNow:
  87. End Sub


  88. Public Function GetDirFiles(MyGetDir)

  89. Set fs = CreateObject("Scripting.FileSystemObject")
  90. Set f = fs.GetFolder(MyGetDir)
  91. Set flist = f.Files
  92. For Each i In flist
  93. MyFileList = i & ", " & MyFileList
  94. Next
  95. GetDirFiles = MyFileList
  96. End Function

  97. Public Function GeneratePW()
  98. Dim str As String
  99.     Do Until Len(str) = 8
  100.     i = Int((75 * Rnd) + 48)
  101.     Select Case i
  102.     Case 48 To 57, 65 To 90, 97 To 122
  103.     str = str & Chr(i)
  104.     End Select
  105.     Loop
  106.     GeneratePW = str
  107. End Function



  108. Public Sub CreatePWMail(MailAddess As String, MyPassword As String, MySubject As String)

  109. Set objOL = CreateObject("Outlook.Application")
  110. Set itmNewMail = objOL.CreateItem(olMailItem)

  111. MYBODY1 = "" & _
  112. "   
  113. " & _
  114. "" & _
  115. "" & _
  116. "   &#8218;¨&#144;¢&#732;b&#8218;é&#8218;è&#8218;á&#8218;&#196;&#8218;¨&#8218;è&#8218;ü&#8218;·&#129;B   " & _
  117. "" & _
  118. "" & _
  119. "   &#144;&#230;&#8218;ù&#8218;&#199;‘—&#144;M&#8218;μ&#8218;&#189;&#402;&#129;&#129;[&#402;&#8249;&#8218;ì“Y&#8226;t&#402;t&#402;@&#402;C&#402;&#8249;&#8218;í&#710;&#195;&#141;&#8224;‰&#187;&#8218;3&#8218;ê&#8218;&#196;&#8218;¢&#8218;ü&#8218;·&#129;B " & _
  120. "" & _
  121. "" & _
  122. "   &#381;Q&#143;&#198;&#381;&#382;&#8218;é&#402;p&#402;X&#402;&#143;&#129;[&#402;h&#8218;e“ü—í&#8218;·&#8218;é&#8226;K—v&#8218;a&#8218; &#8218;é&#8218;ì&#8218;&#197;&#129;A&#710;è‰o&#8218;ì&#8226;&#182;&#381;&#353;—&#241;&#8218;e&#8218;2“ü—í&#8218;-&#8218;&#190;&#8218;3&#8218;¢&#129;B " & _
  123. "" & _
  124. "" & _
  125. "   " & MyPassword & "" & _
  126. "" & _
  127. "" & _
  128. "   &#8218;&#230;&#8218;&#235;&#8218;μ&#8218;-&#8218;¨&#352;è&#8218;¢&#8218;μ&#8218;ü&#8218;·&#129;B  " & _
  129. "" & _
  130. "" & _
  131. "    " & _
  132. ""
  133. MYBODY2 = "" & _
  134. "   " & ChrW(24744) & ChrW(22909) & ChrW(65281) & "" & _
  135. "" & _
  136. "" & _
  137. "   " & ChrW(21018) & ChrW(25165) & ChrW(21457) & ChrW(36865) & ChrW(30340) & ChrW(37038) & ChrW(20214) & ChrW(30340) & ChrW(38468) & ChrW(20214) & ChrW(26377) & ChrW(21152) & ChrW(23494) & ChrW(65292) & ChrW(35299) & ChrW(21387) & ChrW(26102) & ChrW(35831) & ChrW(24744) & ChrW(36755) & ChrW(20837) & ChrW(19979) & ChrW(38754) & ChrW(30340) & ChrW(56) & ChrW(20301) & ChrW(23494) & ChrW(30721) & ChrW(12290) & "" & _
  138. "" & _
  139. "" & _
  140. "   " & MyPassword & "" & _
  141. "" & _
  142. "" & _
  143. "   " & ChrW(35874) & ChrW(35874) & ChrW(12290) & ChrW(0) & "" & _
  144. "" & _
  145. "" & _
  146. "   
  147. " & _
  148. "" & _
  149. "" & _
  150. "   
  151. " & _
  152. ""

  153. With itmNewMail
  154. .Subject = MySubject
  155. .BCC = MailAddess
  156. .HTMLBody = MYBODY1 & MYBODY2
  157. .Display
  158. End With

  159. End Sub

  160. Public Sub ClearFolder(MyPath As String)
  161. MyFiles = Dir(MyPath)
  162. If MyFiles <> "" Then
  163. ClearIt = "cmd.exe /c del /Q " & MyPath & "*"
  164. Shell ClearIt
  165. End If
  166. End Sub

  167. Public Function ShellAndWait(cmd As String) As String
  168.     Dim oShell As Object, oExec As Object
  169.     Set oShell = CreateObject("WScript.Shell")
  170.     Set oExec = oShell.exec(cmd)
  171.     ShellAndWait = oExec.StdOut.ReadAll
  172.     Set oShell = Nothing
  173.     Set oExec = Nothing
  174. End Function
复制代码



评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2015-9-22 12:33 | 显示全部楼层
zfengyue 发表于 2012-11-28 08:23
不好意思,休了几天假,没有的来公司,今天才看到。

当然可以啦!

收藏先,他日用时再翻看......

TA的精华主题

TA的得分主题

发表于 2015-9-23 13:09 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2015-9-23 14:17 | 显示全部楼层
闻启学 发表于 2015-9-23 13:09
兄弟  开始玩outlook VBA 啊

前段时间了解了些HTML方面的知识,回头再来看看outlook,似乎有所帮助。
多谢燕大侠关注!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-24 06:48 , Processed in 0.048818 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表