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
 
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
 
Actually, Michael explains why on his site - there are things you cant change in new inspector but can change in inspector_activate.
 
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
 
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.
Similar threads
Thread starter Title Forum Replies Date
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
Sabastian Samuel HOW DO I FORWARD AN EMAIL WITH MACRO using an email that in the body of another email Outlook VBA and Custom Forms 3
D Print Attachments only in selected emails using a macro Outlook VBA and Custom Forms 3
A Forward Outlook Email by Filtering using Macro Rule Outlook VBA and Custom Forms 44
H send reminder if no reply received on first or original email using macro Using Outlook 2
Diane Poremsky Embed Images in New Messages using a Macro Using Outlook 0
O using macro to send attachments Using Outlook 3
T Using a macro to send email to diffrent address Outlook VBA and Custom Forms 1
S Editing an email with notes and saving it for record using Macro Outlook VBA and Custom Forms 3
reza Macro to automatic reply using orignal mail Outlook VBA and Custom Forms 10
nathandavies How to save emails to a specific folder on a network automatically using a macro Outlook VBA and Custom Forms 14
B Auto reply using macro include original email Using Outlook 4
J Macro generating email using default signature and hyperlink Outlook VBA and Custom Forms 5
M Reply Macro using O365 Connected Account Using Outlook 2
H using macro for sending multiple mail in outlook Outlook VBA and Custom Forms 1
J Specify Font Name or Face using VBA Macro in Word Outlook VBA and Custom Forms 2
R Trigger Application_ItemSend event when sending using Access macro Outlook VBA and Custom Forms 2
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 1
S macro error 4605 Outlook VBA and Custom Forms 0
A Macro Mail Alert Using Outlook 4
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
J Macro to send email as alias Outlook VBA and Custom Forms 0
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
D Outlook 2016 Creating an outlook Macro to select and approve Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to check Cursor & Focus position Outlook VBA and Custom Forms 8
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
M Outlook 2019 Macro not working Outlook VBA and Custom Forms 0
S Outlook 365 Help me create a Macro to make some received emails into tasks? Outlook VBA and Custom Forms 1
Geldner Send / Receive a particular group via macro or single keypress Using Outlook 1
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
V Macro to count flagged messages? Using Outlook 2
sophievldn Looking for a macro that moves completed items from subfolders to other subfolder Outlook VBA and Custom Forms 7
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
E Macro to block senders domain Outlook VBA and Custom Forms 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to answer a mail with attachments Outlook VBA and Custom Forms 2
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
J Macro to Insert a Calendar Outlook VBA and Custom Forms 8

Similar threads

Back
Top