【问题标题】:Saving Outlook Attachement with password then forward使用密码保存 Outlook 附件然后转发
【发布时间】:2015-12-18 19:25:49
【问题描述】:

我需要构建一个规则脚本,将 Outlook 附件(特定于 Excel)保存到用户的硬盘上。然后我需要在这个excel附件中添加密码,然后转发。

使用 VBA Outlook 开发者工具(见下文)保存和转发电子邮件/附件非常简单。但是,我在向此附件添加密码时遇到了问题。这是可能的还是我需要一个外部脚本/程序来完成这项任务?此外,您还有其他建议吗?

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
    saveFolder = "c:\"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
     Call SendEmail
End Sub

Public Sub SendEmail()

    Dim olApp As Outlook.Application
    Dim olMail As Outlook.MailItem
    Dim blRunning As Boolean

     'get application
    blRunning = True
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
        Set olApp = New Outlook.Application
        blRunning = False
    End If
    On Error GoTo 0

    Set olMail = olApp.CreateItem(olMailItem)
    With olMail
        .Subject = "My email with attachment"
        .Recipients.Add "jlanz@mmyemail.com"
        .Attachments.Add "C:\test123.xlsx"
        .Body = "Here is an email"
        .Send 
    End With
If Not blRunning Then olApp.Quit

Set olApp = Nothing
    Set olMail = Nothing

End Sub

【问题讨论】:

  • 我怀疑你需要一个外部程序来解决这个问题。
  • 添加对Excel库的引用然后打开文件,添加密码,保存,然后附加发送。

标签: vba outlook


【解决方案1】:

根据我的评论,您需要添加对 excel 库的引用。然后您可以使用下面的示例设置密码。

Sub ProtectExcelWorkbook(filePath As String)
Dim pw As String
pw = "password"

    Dim eApp As Excel.Application
    Dim eBook As Excel.Workbook
    Set eApp = New Excel.Application
    Set eBook = eApp.Workbooks.Open(filePath)    
    eBook.Password = pw
    eBook.Save

    Set eBook = Nothing
    eApp.Quit
    Set eApp = Nothing

End Sub


Public Sub saveAttachtoDisk(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
    saveFolder = "c:\"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          ProtectExcelWorkbook saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
     Call SendEmail
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 2022-01-17
    • 2013-11-17
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    相关资源
    最近更新 更多