Regarding this question Automatically open a copy of the attached file ,and the accepted answer by Mr @FaneDuru.
I need after I edited the opened workbook,
then delete the attached file and save (add) the edited workbook into the email itself.
I wish to fulfill that task from excel workbook itself
by adding code to the event BeforeClose
.
I manged to remove the attached file.
and I know the code of adding attachment to the email, but I do not know how to use it from excel workbook itself.
In advance,grateful for all useful comments and answers.
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