[VBA] complicated(?) outlook events - need help with code

Status
Not open for further replies.

RobZ

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hello everyone! I am having two problems with outlook (precisely a shared mailbox) that I am trying to solve and so far have been unsuccessful. Maybe your expertise will come in handy. At the office we are using a SHARED mailbox as well as several excel based vba scripts to gather statistics. One statistic that we collect is mean time to reply, calculated as the difference between time replied to an email (accessed through propert accessor) and creation time. The problem here is that if a user opens an email and saves it, outlook will automatically mark it as replied, which can later lead to a disaster. My solution to this problem was to write a write event handler ( see below for a snippet)
Code:
Private Sub m_objMail_Write(Cancel As Boolean)     Reply = MsgBox("wanna save?", vbYesNo)         If Reply = vbNo Then             Cancel = True         End If End Sub
but it fires even when you send an email, even two times, so it's super annoying. How else can I solve the problem? The second thing is that when we reply to an email the from field defaults to the from email address. I also tried to fix that but none of the below works...
Code:
Private Sub m_objMail_Reply(ByVal Response As Object, Cancel As Boolean)     With m_objMail          '.SendUsingAccount = "test@gmail.com"          '.SentOnBehalfOfName =  "test@gmail.com"          .Sender.Address =  "test@gmail.com"     End With End Sub
Any ideas if it can be changed to a different email address> rEGARDS, Rob
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
The only way to solve the draft problem is to not save drafts...

On the address, SentOnBehalfOfName is what you need to use to send from the shared mailboxes. See http://www.slipstick.com/developer/reply-using-address-message-sent-to/ for a macro that might work for you. Or try using it with your macro.

This works for open or selected items -

Code:
Option Explicit 
 
Private WithEvents oExpl As Explorer 
 
Private WithEvents oItem As MailItem 
 
Private bDiscardEvents As Boolean 
 
Private Sub Application_Startup()
  Set oExpl = Application.ActiveExplorer
  bDiscardEvents = False 
 
End Sub 
 
Private Sub oExpl_SelectionChange()
  On Error Resume Next
  Set oItem = oExpl.Selection.Item(1) 
 
End Sub 
 
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
   
  Cancel = True
  bDiscardEvents = True
  Dim oResponse As MailItem
  Set oResponse = oItem.Reply
  oResponse.SentOnBehalfOfName = "me@here.com"
  oResponse.Display
  
  bDiscardEvents = False 
 
Set oItem = Nothing 
 
End Sub
 

RobZ

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hi Diane,

Thanks a lot for your reply. It does work in a sense - meaning it does set the acc in the from field but still sends the email with a different account :-(
 

Forum Admin

Senior Member
Do the sender's have Send As permission on the mailbox? I assumed they did - and if they can change the From field manually and it works, they do.
 

Forum Admin

Senior Member
What version of exchange are you using? I'll double check with Outlook 2010, but in 2013, with my account set as owner and having send as permissions, the shared mailbox is set as the reply account automatically. No VBA necessary.
 

RobZ

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Do the sender's have Send As permission on the mailbox? I assumed they did - and if they can change the From field manually and it works, they do.

Yes, they do. If you change the field manually it does work.

What version of exchange are you using? I'll double check with Outlook 2010, but in 2013, with my account set as owner and having send as permissions, the shared mailbox is set as the reply account automatically. No VBA necessary.

Either 2007 or 2010(is there an easy way to find out?). Definitely not 2013. When you reply/forward an email outlook automatically inserts the groups email address and we have to change it... I am looking for an automated solution :)

Your help is much appreciated :)

Kind regards.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
When you reply/forward an email outlook automatically inserts the groups email address and we have to change it... I am looking for an automated solution :)

HElp > About will have the version - but i can test with both 2010 and 2007.

What address do you want in the From field? The Shared mailbox, or the user's own address?
 

RobZ

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
HElp > About will have the version - but i can test with both 2010 and 2007. What address do you want in the From field? The Shared mailbox, or the user's own address?
User's own address always. Kind regards
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Ah... I had it in my head that you wanted to use the shared address. The code needs tweaked a bit... or remove send as permission from the mailbox.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This works here to change the shared address to the user's mailbox address.

Code:
Option Explicit 
 
Private WithEvents oExpl As Explorer 
 
Private WithEvents oItem As MailItem 
 
Private bDiscardEvents As Boolean 
 
Private Sub Application_Startup()
  Set oExpl = Application.ActiveExplorer
  bDiscardEvents = False 
 
End Sub 
 
Private Sub oExpl_SelectionChange()
  On Error Resume Next
  Set oItem = oExpl.Selection.Item(1) 
 
End Sub 
 
Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
    
  Cancel = True
  bDiscardEvents = True
  Dim oResponse As MailItem

   Set oResponse = oItem.Reply
          oResponse.SentOnBehalfOfName = "dianep@outlookmvp.com"
   oResponse.Display

  bDiscardEvents = False 
 
Set oItem = Nothing 
 
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Hmmm. That is the same code i posted before. It's working here. It's triggered in open messages or on a change event. So try selecting a different message then reply.
 

RobZ

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hmmm. That is the same code i posted before. It's working here. It's triggered in open messages or on a change event. So try selecting a different message then reply.
The code does change the from field to what's specified in ".sentonbehalfof" but it doesn't, say, validate to anything. If you click check names, nothing happens. If you send an email like this it will get sent as shared mailboxe's mail. If you double click the changed email from the from field, it shows the display name of the shared mailbox.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It worked for me once using slightly difference code and i removed the extra code because I thought it wasn't needed. OOps. I'll add the code back and try it again.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Back to the code i used last night when it worked - use this for the reply block.

Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
Dim oAccount As Outlook.Account
Dim oResponse As MailItem
Cancel = True
bDiscardEvents = True



For Each oAccount In Application.Session.Accounts
If oAccount = "dianep@outlookmvp.com" Then
Set oResponse = oItem.Reply
oResponse.SendUsingAccount = oAccount
oResponse.Display
End If

Next
bDiscardEvents = False

Set oItem = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Grrr.... i goofed on my testing (had the wrong account selected), so no, this might not work. Damn. Must get more coffee so I pay better attention to what i'm doing. :)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I give up, for now at least. If they never need to send as the shared account, you could remove Send As permission... that will cause replies to default to the user's address.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
O VBA Cases with Listbox - Can you use Multi-Select? Outlook VBA and Custom Forms 4
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
V Modifying the built in forms with VBA Outlook VBA and Custom Forms 4
S Excel VBA and shared calendar issue Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 3
L VBA unknown character Outlook VBA and Custom Forms 2
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
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
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0

Similar threads

Top