[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.
Top