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.
Thread starter Similar threads 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 New Slipstick.com Articles 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
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 3
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 Calendar Printing Assistant 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
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
4 Macro to set the category of Deleted Item? Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
V Outlook Macro to show Flagged messages Outlook VBA and Custom Forms 2
O Run macro automatically at sending an email Using Outlook 11
R Retain Original Message When Forwarding With Macro Outlook VBA and Custom Forms 3
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
B Reply and replyall macro is not working Outlook VBA and Custom Forms 1
O Macro - paste as plain text Outlook VBA and Custom Forms 2
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
witzker Macro to set contact reminder to next day 9:00 Outlook VBA and Custom Forms 45
M Adding Macro to populate "to" "subject" "body" not deleting email string below. Outlook VBA and Custom Forms 5
E Copying data from e-mail attachement to EXCEL file via macro Outlook VBA and Custom Forms 38
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
Similar threads


















































Top