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.
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
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
D Autosort macro for items in a view Outlook VBA and Custom Forms 2
S HTML to Plain Text Macro - Help Outlook VBA and Custom Forms 1
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
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
S Visual indicator of a certain property or to show a macro toggle Outlook VBA and Custom Forms 2
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 8
S Macro for Loop through outlook unread emails Outlook VBA and Custom Forms 2
Globalforester ItemAdd Macro - multiple emails Outlook VBA and Custom Forms 3
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
witzker HowTo start a macro with an Button in OL contact form Outlook VBA and Custom Forms 12
witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 4
U Macro for reminders,tasks,calendar Outlook VBA and Custom Forms 4
V macro runs slower on startup than after Outlook VBA and Custom Forms 3
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
G VBA Macro Using Outlook 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
R Macro Schedule every day in Outlook Using Outlook 0
Healy Consultants Macro to remove inside organization distribution list email address when reply to all recepients Outlook VBA and Custom Forms 0
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5

Similar threads

Top