Macro using .SendUsingAccount only works the first time, after starting Outlook

Status
Not open for further replies.

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Hi All,

I've put a macro together from examples I found on the web, which uses the Inspectors.NewInspector event to run code when a new item window opens, whether New, Reply, ReplyAll or Forward. Amongst other things, the macro conditionally changes the value in the 'From' drop-down list, using the MailItem.SendUsingAccount property.

The problem I have is that the setting the SendUsingAccount property seems to work when the macro runs for the very first time, after starting Outlook, but in all subsequent runs of the macro the MailItem.SendUsingAccount line fails to set the value. The MsgBox I included for test purposes appears every time, so I know the macro runs on each occasion.

Could anyone show me what needs to be done with this macro to get it working every time please? Below is a cut down version of the macro, to keep things simple.

Kind regards,

Steve

Dim WithEvents colInsp As Outlook.Inspectors

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objMail As Outlook.MailItem
Dim OutApp As Outlook.Application
Set OutApp = Application

If Inspector.CurrentItem.Class = olMail Then
Set objMail = Inspector.CurrentItem
If objMail.Sent = False Then
With objMail
MsgBox "Setting SendAccount Next"
objMail.SendUsingAccount = OutApp.Session.Accounts.Item(5)
End With
End If
End If
Set objMail = Nothing
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
i don't know why your code doesn't work, but this is working here with pop accounts (if i hit reply to opened messages, but not in reading pane). i put your code together with Use a Default Subject for New Messages - which is based on Michael's NewInspector and Inspector_Activate - VBOffice

Code:
Private WithEvents m_Inspectors As Outlook.Inspectors
Private WithEvents m_Inspector As Outlook.Inspector

Private Sub Application_Startup()
  Set m_Inspectors = Application.Inspectors
End Sub

Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
  Set m_Inspector = Inspector
End Sub

Private Sub m_Inspector_Activate()
If TypeName(m_Inspector.CurrentItem) = "MailItem" And _
m_Inspector.CurrentItem.Sent = False Then

Dim objMail As Outlook.MailItem
Dim OutApp As Outlook.Application
Set OutApp = Application
Set objMail = m_Inspector.CurrentItem
If objMail.Sent = False Then
    MsgBox "Setting SendAccount Next"
    objMail.SendUsingAccount = OutApp.Session.Accounts.Item(2)
End If

End If
  Set m_Inspector = Nothing
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Actually, Michael explains why on his site - there are things you cant change in new inspector but can change in inspector_activate.
 

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Many thanks Diane, I'm very grateful to you!

I'll play around with the macro you've provided above and see if I can get it working on my machine. As long as it does, I'm fairly confident I can move the rest of my macro code into it and I should then have the finished, working article!

Will keep you posted. Thanks again.

Kind regards,

Steve
 

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Hi again Diane,

Having dropped your example macro into my Outlook environment, I'm pleased to confirm that it appears to be working great with my IMAP Outlook.com accounts and for successive new emails, replies, reply-alls and forwards!

Once I've got my end result working as intended, I'll post it back here with an explanation as to what does and why I needed it. You never know, it might come in handy for a few other people who may have bumped into a similar situation or set of requirements as me and, based on the fact that the majority of it has been plagerised from other people's stuff and help from you, I feel it should be openly available by default!

Kind regards,

Steve
 
Status
Not open for further replies.
Top