从 excel 工作簿本身向 O​​utlook 电子邮件添加附件(使用 excel 事件 'BeforeClose')

Add attachment to outlook email from excel workbook itself (using excel event 'BeforeClose')

关于这个问题,以及@FaneDuru先生接受的答案。

我需要编辑打开的工作簿后,
然后删除附件并将编辑后的工作簿保存(添加)到电子邮件中。
我希望通过向 事件 BeforeClose.
添加代码来完成 excel workbook itself 的任务 我设法删除了附件。
我知道在电子邮件中添加附件的代码,但我不知道如何从 excel 工作簿本身使用它。
预先感谢所有有用的评论和答案。

Option Explicit
Option Compare Text

Public WithEvents myItem As Outlook.MailItem
Public EventsDisable As Boolean

Private Sub Application_ItemLoad(ByVal Item As Object)
    If EventsDisable = True Then Exit Sub
    If Item.Class = olMail Then
        Set myItem = Item
    End If
End Sub

Private Sub myItem_Open(Cancel As Boolean)
    EventsDisable = True
        If myItem.Subject = "Auto Plan" And Application.ActiveExplorer.CurrentFolder.Name = "MyTemplate" Then
            If myItem.Attachments.Count > 0 Then
                Dim obAttach As Attachment, strSaveMail As String, objExcel As Object
                  Set obAttach = myItem.Attachments(1)
                    strSaveMail = "C:\Users\Waleed\Desktop\outlook-attachments\"
                     obAttach.SaveAsFile strSaveMail & obAttach.DisplayName
                      Dim obAttachName As String
                       obAttachName = obAttach.FileName
                     
                     obAttach.Delete 'Remove attached file
                     
                     Set objExcel = CreateObject("Excel.Application")
                     objExcel.Workbooks.Open strSaveMail & obAttach.DisplayName
                  
                  'Add the below line to workbook itself on event (BeforeClose)
                  myItem.Attachments.Add strSaveMail & obAttachName
                  
                objExcel.Visible = True
               'AppActivate objExcel.ActiveWindow.Caption 'using AppActivate causes error
              Set objExcel = Nothing
            End If
        End If
    EventsDisable = False
End Sub

为了完成整个过程,您应该按照我将尝试解释的方式进行。

首先,我想做一个简短的描述,描述调用的过程: 1. 从模板所在的位置打开模板。 2. 使用触发 myItem_Open 的 Outlook Application_ItemLoad 事件保存附件,将其删除(从邮件中)并在 Microsoft Excel 中打开它。 3、你修改保存的附件,保存,回到邮件window按Send。 4. Outlook ItemSend 事件将重新附加以前保存的工作簿(现已修改)并发送包含保存的工作簿的邮件。

  1. 请在 ThisOutlookSession 代码模块顶部(在声明区域)复制接下来的两个变量声明:
Private Const strSaveMail As String = "C:\Users\Waleed\Desktop\outlook-attachments\"
Private wbName As String 'to keep the attachment name

删除 strSaveMail As String 声明,strSaveMail = "C:\Users\Waleed\Desktop\outlook-attachments\" 并将附件工作簿名称从 myItem_Open 代码中指定给 wbName

  1. 改编后的代码事件应如下所示:
Private Sub myItem_Open(Cancel As Boolean)
    EventsDisable = True
        If myItem.Subject = "Auto Plan" And Application.ActiveExplorer.CurrentFolder.Name = "MyTemplate" Then
            If myItem.Attachments.Count > 0 Then
                Dim obAttach As Attachment, objExcel As Object
                Set obAttach = myItem.Attachments(1)
                obAttach.SaveAsFile strSaveMail & obAttach.DisplayName
                wbName = obAttach.DisplayName 'to be used later, when the workbook will be reattached                     
                obAttach.Delete 'Remove attached file
                     
                Set objExcel = CreateObject("Excel.Application")
                objExcel.Workbooks.Open strSaveMail & wbName                  
                objExcel.Visible = True
                Set objExcel = Nothing
            End If
        End If
    EventsDisable = False
End Sub
  1. 修改之前保存并打开的工作簿,保存然后发回邮件window然后Send。将触发 Outlook ItemSend 事件并将 re-attach 保存的工作簿:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
   If TypeName(Item) = "MailItem" Then
        Set MyItem = Item
        On Error GoTo Err_Handler
        If MyItem.Subject = "Auto Plan" And MyItem.Attachments.Count = 0 Then
            MyItem.Attachments.Add strSaveMail & wbName, 1 
        End If
   End If
   Exit Sub
Err_Handler:
    MsgBox Err.Number & vbCrLf & Err.Description
   Cancel = True 'if an error will be raised, the mail sending is cancelled, to see what problem does appear...
End Sub

邮件将作为附件与修改后的工作簿一起发送。

使用全局变量会很好,如果没有任何错误(在测试期间)会出现。因此,即使出现错误,它们都可以保存在注册表中并且可以毫无问题地使用。

可以修改 Open 事件,使 Excel 在最大化 window 中打开(附加)工作簿,就在邮件 window 之后。

返回邮件 window 也可以自动进行,正如您在上一个问题的回答中看到的那样。

如果有任何不够清楚的地方,请不要犹豫,要求澄清。

但请按原样尝试 code/solution ,只有在看到它工作后才尝试修改它,如有必要...