关于这个问题Automatically open a copy of the attached file ,以及@FaneDuru 先生接受的答案。
我需要在编辑打开的工作簿后,
然后删除附件并将编辑的工作簿保存(添加)到电子邮件本身。
我希望从 excel workbook itself
完成这项任务通过将代码添加到 事件 BeforeClose
.
我设法删除了附件。
我知道在电子邮件中添加附件的代码,但我不知道如何从 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.使用Outlook Application_ItemLoad
触发 myItem_Open
的事件,保存附件,将其删除(从邮件中)并在 Microsoft Excel 中打开它。 3.您修改保存的附件,保存,返回邮件窗口并按Send
. 4. 展望ItemSend
事件将重新附加以前保存的工作簿(现在已修改),并且将发送包含保存的工作簿的邮件。
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\"
并将附件工作簿名称提供给 wbName
来自 myItem_Open
代码。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
Send
.展望 ItemSend
事件将被触发并重新附加保存的工作簿: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 - 从 excel 工作簿本身向 Outlook 电子邮件添加附件(使用 excel 事件 'BeforeClose' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72001372/