Capturing Send Variables without using Application_ItemSend in ThisOutlookSession

Status
Not open for further replies.

accessDatabase

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
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!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

accessDatabase

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

accessDatabase

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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 = ""
 

accessDatabase

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
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?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Either restart outlook or click on the application_startup macro and click Run.
 
Status
Not open for further replies.
Top