PDA

View Full Version : [SOLVED:] VBA - Message Input Box Before Sent Disappeared



fleety1991
01-11-2024, 03:08 AM
Hi,

I'm new to VBA and I've tried to create code that before sending an e-mail with an attachment, a message box appears asking if the attachment has been checked before sending.

This worked on the day it was implemented, but after restarting my PC, the code is still visible in ThisOutlookSession but does not run.

Is there something I need to do in order for this to run each day?

I also want to advance this to only run this if the attachment/s are specific attachment types, not sure if this is possible or not.

Any help is appreciated.



Code below:


'ThisOutlookSession
Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim msg As String
Dim att As Outlook.Attachment

If TypeOf Item Is Outlook.MailItem Then
If Item.Attachments.Count > 0 Then
msg = "Have you double checked the attachment/s to ensure that the data is relevant?." & vbNewLine & _
"If no, please press the No button and re-check." & vbNewLine
For Each att In Item.Attachments
msg = msg & vbNewLine & "* " & att.FileName
Next
If MsgBox(msg, vbQuestion + vbSystemModal + vbYesNo) = vbNo Then Cancel = True
End If
End If
End Sub

jdelano
01-11-2024, 09:05 AM
Have you used debugging to see if the code is running but the If is failing?

VBA: How to Debug Code - Overview, Tools, Shortcut Keys (corporatefinanceinstitute.com) (https://corporatefinanceinstitute.com/resources/excel/vba-how-to-debug-code/)

Aussiebear
01-11-2024, 07:31 PM
Just guessing here, so test before you intend to use in your outlook program



Sub SendMail()
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.To = "address@domain.com"
.CC = ""
.BCC = ""
.Subject = "File for your perusal"
.BodyFormat = olFormatHTML
.HTMLBody = "Hi, <p> I'm sending this message from Excel using VBA.</p>Please _
find file attached."
.Attachments.Add ActiveWorkbook.FullName
.Importance = olImportanceHigh
.ReadReceiptRequested = True
If .Attachments.Count >0 then
If Msgbox” Is the attachment relevant to the person receiving this email?, + vbYesNo + vbQuestion, “Test for Validity”) = Yes Then
.Send
Else
Exit sub
End if
End if
End With
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub

fleety1991
01-12-2024, 06:17 AM
Thank you both for your replies, it's appreciated!

It's strange because it seems to be working without any issues today, so not quite sure why yesterday was different. I'll monitor it and I'll take the advice given if it is inconsistent with when it works.

Thank you!