EXCEL VBA, remitente manual de correo electrónico de Outlook, problema del módulo de clase
Todavía estoy trabajando en el problema que describí en miPrimera pregunta sobre este tema. Para una breve actualización, es un archivo de Excel que contiene la lista de plantillas de correo electrónico y archivos adjuntos, a cada unidad de la lista, agregué el botón que abre la plantilla de la unidad de donación, realizo algunos cambios, luego adjunta archivos y muestra el correo al Usuario. El usuario puede modificar el correo si es necesario y luego enviar o no enviar correo. He intentado varios enfoques que se describen a continuación. Desafortunadamente, ahora estoy estancado en el problema con el módulo de clase, que describió brevementeaquí. He creado un módulo de clase, como 'EmailWatcher' e incluso hago una pequeña combinación con el método descritoaquí:
Option Explicit
Public WithEvents TheMail As Outlook.MailItem
Private Sub Class_Terminate()
Debug.Print "Terminate " & Now()
End Sub
Public Sub INIT(x As Outlook.MailItem)
Set TheMail = x
End Sub
Private Sub x_Send(Cancel As Boolean)
Debug.Print "Send " & Now()
ThisWorkbook.Worksheets(1).Range("J5") = Now()
'enter code here
End Sub
Private Sub Class_Initialize()
Debug.Print "Initialize " & Now()
End Sub
El cambio al siguiente formulario no hace ningún cambio:
Option Explicit
Public WithEvents TheMail As Outlook.MailItem
Private Sub Class_Terminate()
Debug.Print "Terminate " & Now()
End Sub
Public Sub INIT(x As Outlook.MailItem)
Set TheMail = x
End Sub
Private Sub TheMail_Send(Cancel As Boolean)
Debug.Print "Send " & Now()
ThisWorkbook.Worksheets(1).Range("J5") = Now()
'enter code here
End Sub
Private Sub Class_Initialize()
Debug.Print "Initialize " & Now()
End Sub
El código del módulo es el siguiente:
Public Sub SendTo()
Dim r, c As Integer
Dim b As Object
Set b = ActiveSheet.Buttons(Application.Caller)
With b.TopLeftCell
r = .Row
c = .Column
End With
Dim filename As String, subject1 As String, path1, path2, wb As String
Dim wbk As Workbook
filename = ThisWorkbook.Worksheets(1).Cells(r, c + 5)
path1 = Application.ThisWorkbook.Path &
ThisWorkbook.Worksheets(1).Range("F4")
path2 = Application.ThisWorkbook.Path &
ThisWorkbook.Worksheets(1).Range("F6")
wb = ThisWorkbook.Worksheets(1).Cells(r, c + 8)
Dim outapp As Outlook.Application
Dim oMail As Outlook.MailItem
Set outapp = New Outlook.Application
Set oMail = outapp.CreateItemFromTemplate(path1 & filename)
subject1 = oMail.subject
subject1 = Left(subject1, Len(subject1) - 10) &
Format(ThisWorkbook.Worksheets(1).Range("D7"), "DD/MM/YYYY")
oMail.Display
Dim CurrWatcher As EmailWatcher
Set CurrWatcher = New EmailWatcher
CurrWatcher.INIT oMail
Set CurrWatcher.TheMail = oMail
Set wbk = Workbooks.Open(filename:=path2 & wb)
wbk.Worksheets(1).Range("I4") =
ThisWorkbook.Worksheets(1).Range("D7").Value
wbk.Close True
ThisWorkbook.Worksheets(1).Cells(r, c + 4) = subject1
With oMail
.subject = subject1
.Attachments.Add (path2 & wb)
End With
With ThisWorkbook.Worksheets(1).Cells(r, c - 2)
.Value = Now
.Font.Color = vbWhite
End With
With ThisWorkbook.Worksheets(1).Cells(r, c - 1)
.Value = "Was opened"
.Select
End With
End Sub
Finalmente, hice una clase que funciona y puse algunos controles para verificarla, como puede ver en el código del módulo de clase. Pero el problema es que no detecta el evento Enviar. La clase está terminando al final del sub. Dejar el correo electrónico completamente al usuario. La pregunta es: ¿dónde está el error? ¿O cómo dejar el módulo de clase en el llamado "modo de espera", o tal vez alguna otra sugerencia? También considero la forma de buscar correos en la 'bandeja de salida', pero el enfoque con el evento Enviar es mucho más favorable.