Capturing Send Variables without using Application_ItemSend in ThisOutlookSession

Status
Not open for further replies.
Outlook version
Outlook 2016 64 bit
Email Account
IMAP
Hello,

I want to place a custom button in the send window's ribbon which allows me to capture the sender, recipient, subject, body and date/time sent and pass these through to an Access database then send the email as normal.

I have found out how to get these variables using the Application_ItemSend event in ThisOutlookSession. However, I don't want it to happen each time, only when the user selects the Custom button instead of the standard Sent button.

Here's the code I've been using in ThisOutlookSession which has been getting the variables I need:
Code:
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim thisMail As Outlook.MailItem
Dim senderEmail As Variant
Const PR_SMTP_ADDRESS     As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"

With Item
Debug.Print .subject
Debug.Print .body
Debug.Print .senderEmailAddress
Debug.Print "Sent On: " & Now()
Set recips = .recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
Debug.Print pa.GetProperty(PR_SMTP_ADDRESS) 'recip.Name & " " &
Next
End With

Thanks in advance!
 
There are a couple of ways to do it, but they all use the same basic idea - an If statement...

If you want users to say 'send to access' before sending, use two macros - one to set a value that says 'yes' and add an if statement to the itemsend - if the variable = yes then send to access. The other way is to bring up a dialog each time asking. The if statement sends to access if yes, skips it if no.

Example from How to set a flag to follow up using VBA
prompt$ = "Do you want to flag this message for followup?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Add flag?") = vbYes Then
' do flag
end if
 
Hi Diane, thanks for your reply.

When I paste this code into a module I get a compile error on WithEvents saying Only Valid in Object Module. Which parts go in which type of module?

Option Explicit
Dim SetFlag
Private WithEvents olSentItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set olSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
Set objNS = Nothing
End Sub

Private Sub olSentItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Dim prompt As String
If SetFlag = vbYes Then

With Item
.MarkAsTask olMarkThisWeek
' sets a due date in 3 days
.TaskDueDate = Now + 3
.ReminderSet = True
.ReminderTime = Now + 2
.Save
End With
End If
SetFlag = vbNo
End Sub

Sub SayYes()
SetFlag = vbYes
End Sub
 
did you paste it into thisoutlooksession or a new module? Those macro only work in thisoutlooksession (because they are automatic) - the error is telling you withevents only works in thisoutlooksession.
 
Hi,

I've put this code into thisOutlookSession and assigned 'Say Yes' to a custom button on the ribbon. I click that button, then click the Send button, but it's not firing the code. Any suggestions?
Code:
Option Explicit

Dim SetFlag
Private WithEvents olSentItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set olSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
Set objNS = Nothing
End Sub

Private Sub olSentItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Dim prompt As String
Dim recips As Outlook.recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS     As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
If SetFlag = vbYes Then
    With Item
    Debug.Print .subject
    Debug.Print .body
    Debug.Print .senderEmailAddress
    Debug.Print "SentOn: " & Now()
 
          Set recips = .recipients
          For Each recip In recips
            Set pa = recip.PropertyAccessor
            Debug.Print pa.GetProperty(PR_SMTP_ADDRESS)
          Next
 
    End With
End If
SetFlag = vbNo
End Sub

Sub SayYes()
SetFlag = vbYes
End Sub

Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
         
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
     
    Set objApp = Nothing
End Function
 
if you are using a button, set a value -
Sub SayYes()
SetFlag = "Yes"
End Sub

then use
If SetFlag = "Yes" Then

clear it using
SetFlag = ""
 
Hi Diane, yes I've got an IF block in there, unfortunately the Private Sub olSentItems_ItemAdd(ByVal Item As Object) isn't being fired after I've sent an email. Perhaps I've got it set up wrong?
 
Either restart outlook or click on the application_startup macro and click Run.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Capturing forward event when multiple items are selected Using Outlook 0
D Capturing Data from a form into excel Outlook VBA and Custom Forms 1
B Capturing form data Outlook VBA and Custom Forms 1
S Re: Capturing Outlook Forms Events Outlook VBA and Custom Forms 1
D Capturing Outlook Forms Events Outlook VBA and Custom Forms 21
C Synchronization and taking forever to send Using Outlook 2
D Send on behalf of does not store the base mailbox Using Outlook 0
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
S Outlook 2002- "Send" button has disappeared. Help please. Using Outlook 1
TomHuckstep Remove Send/Receive All Folders (IMAP/POP) button from Outlook 365 Ribbon Using Outlook 2
J Macro to send email as alias Outlook VBA and Custom Forms 0
F Add a category before "Send an Email When You Add an Appointment to Your Calendar" Outlook VBA and Custom Forms 0
W Outlook 365 I am getting the "Either there is no default mail client" error when I try to send an email on excel Office 365 Using Outlook 1
T Outlook 2010 recipient no longer shows in 'Send To' Using Outlook 0
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Geldner Tweak Junk Email Reporting tool to default to particular email on send? Using Outlook 3
Geldner Send / Receive a particular group via macro or single keypress Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
L How to avoid issues with "Send on Behalf" Using Outlook 3
M Outlook 365 refuses to send email Using Outlook 0
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
M I cant send emails via Outlook in my W10 PC. Using Outlook 3
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
G Send a greeting message to a contact on birthday Outlook VBA and Custom Forms 5
T Outlook creates a copie of every mail I send Using Outlook.com accounts in Outlook 4
R How to restrict GWSMO sync to Outlook Send/Receive cycles Using Outlook 0
M Outlook, send to > mail recipient - results in plain text email Using Outlook 1
A Unflag Inbox and Flag Inbox with Orange Category After Item is send Outlook VBA and Custom Forms 3
glnz O365 - How to send from acct 2 but showing email name from acct 1 as From - alias? Using Outlook 0
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
C Synchronizing subscribed folders causes hanging during send/receive process Using Outlook 2
M Extract all links from Outlook email, send to Excel Using Outlook 2
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Outlook 365 - suddenly unable to send using Gmail POP3 Using Outlook 10
T After I send a new email, it remains in the Draft folder Using Outlook.com accounts in Outlook 3
B Programmatically force html send and insert clipboard contents into body Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M ERROR: None of your email accounts could send to this recipient Using Outlook 2
C Send/receive error 80040119 Using Outlook 2
J Send Again NDR Outlook VBA and Custom Forms 1
J Add an Attachment Using an Array and Match first 17 Letters to Matching Template .oft to Send eMail Outlook VBA and Custom Forms 2
ChrisK2 Send email to advertise@slipstick.com fails: "The group advertising isn't set up to receive messages from..." Using Outlook 3
B resend if no reply and send an automatic reminder Outlook VBA and Custom Forms 0
F Send As a Gmail account via outlook Web Using Outlook 3
R auto send email when meeting closes from a shared calendar only Outlook VBA and Custom Forms 2
X Unable to send an email from one account to another on same PC Using Outlook 2
S Meeting Invite arrives from Wrong ("send-as") Sender Using Outlook 1
M VBA to send reminder email if no response Using Outlook 13

Similar threads

Back
Top