Outlook 365 Outlook Crashes on setting SaveSentMessageFolder

AlphonseG

New Member
Outlook version
Outlook 365 32 bit
Email Account
Office 365 Exchange
Using Outlook Desktop 365 with Office 365 Exchange. I have a single account with multiple shared mailboxes.
On replying to received messages I want to send from the shared mailbox that the message was addressed to.
When composing a new message, I want to set the SentOnBehalfOfName based on the current folder and save the sent message in the same folder.
With many, many hours of research and trials, I've come up with the below code.
Everything works as desired EXCEPT, saving the new message in the current folder. Setting SaveSentMessageFolder causes Outlook to crash and restart, disabling all VBA on restarting.
Setting it manually works fine. How can I set the SaveSentMessageFolder without crashing?

Code:
Public WithEvents myItem As Outlook.MailItem

Public WithEvents myOlInspectors As Outlook.Inspectors



Private Const conDefaultSendAddress As String = "mydefaultaddress"



Sub Initialize_Handler()

   On Error Resume Next

   Set myOlInspectors = Application.Inspectors

   If TypeName(Application.ActiveWindow) = "Explorer" Then

      Set myItem = Application.ActiveExplorer.Selection.item(1)

   ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then

      Set myItem = Application.ActiveInspector.CurrentItem

   Else

      Set myItem = Nothing

   End If

End Sub



Private Sub Application_Startup()

   Initialize_Handler   '11/25/2022

End Sub



Private Sub myItem_Open(Cancel As Boolean)

'Set Send address and save folder for new email

Dim aE As Outlook.addressEntry

Dim strSaveFolder As String

Dim strFrom As String

Dim oFldr As Outlook.Folder



   If myItem.Recipients.Count = 0 Then

       strFrom = SendAddressForFolder

       myItem.Recipients.Add (strFrom) 'Issue with SentOnBehalfOfName

       myItem.Recipients.ResolveAll

       Set aE = myItem.Recipients(1).addressEntry

       myItem.Recipients.Remove (1)

       myItem.SentOnBehalfOfName = strFrom

       myItem.Sender = aE

       strSaveFolder = NewMailSaveFolder

       If strSaveFolder <> "" Then

         Set oFldr = FindOlFolder(Application.Session.Folders(1), strSaveFolder)

         Set myItem.SaveSentMessageFolder = oFldr 'crashes here

       End If

    End If

End Sub



Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)

   Dim msg As Outlook.MailItem

 

   If Inspector.CurrentItem.Class = olMail Then

      Set msg = Inspector.CurrentItem

      If msg.Size = 0 Then

         Set myItem = Inspector.CurrentItem

         'can't set SentOnBehalfOfName here, it does not display

      End If

   End If

End Sub



Private Sub myItem_Forward(ByVal Forward As Object, Cancel As Boolean)

   Call AccountToSendViaEx(Forward, False)

End Sub



Private Sub myItem_Reply(ByVal Response As Object, Cancel As Boolean)

   Call AccountToSendViaEx(Response, True)

   Debug.Print "reply"

End Sub



Private Sub myItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)

   Call AccountToSendViaEx(Response, True)

End Sub



Private Sub AccountToSendViaEx(ByRef Response As Object, ByVal replyAll As Boolean)

   Response.SentOnBehalfOfName = ReplyAddressUse

End Sub



Private Function ReplyAddressUse() As String

      Dim intRet As Integer

      Dim strAddress As String

      Dim recips As Outlook.Recipients

      Dim recip As Outlook.Recipient

      Dim pa As Outlook.PropertyAccessor

      Dim strRet As String

      Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"

        

10    On Error GoTo ErrLine

20       Set recips = myItem.Recipients

30       For Each recip In recips

40          Set pa = recip.PropertyAccessor

50          strAddress = pa.GetProperty(PR_SMTP_ADDRESS)

60          If strAddress Like "*@mydomain*" Then

70             strRet = strAddress

80             Exit For

90          End If

100      Next recip

110      If strRet <> "" Then

120         ReplyAddressUse = strRet

130      Else

140         ReplyAddressUse = SendAddressForFolder

150      End If

ExitLine:

160      On Error Resume Next

170      Set recips = Nothing

180      Set recip = Nothing

190      Set pa = Nothing

200      Exit Function



ErrLine:

210      MsgBox "Error: " & Err.Number & vbCrLf & Err.Description & vbCrLf & "Line: " & Erl

220      Resume ExitLine

        

End Function



Private Function SendAddressForFolder() As String

Dim strCurrentFolder As String

Dim strRet As String

   strCurrentFolder = Application.ActiveExplorer.CurrentFolder

   Select Case strCurrentFolder

   Case "mydesiredfolder1"

      strRet = "mysharedmailbox1"

   Case "mydesiredfolder2"

      strRet = "mysharedmailbox2"

   Case "mydesiredfolder3"

      strRet = "mysharedmailbox3"

   Case Else

      strRet = conDefaultSendAddress

   End Select

SendAddressForFolder = strRet

End Function



Private Function NewMailSaveFolder() As String

Dim strCurrentFolder As String

Dim strRet As String

   strCurrentFolder = Application.ActiveExplorer.CurrentFolder

   Select Case strCurrentFolder

   Case "mydesiredfolder1"

      strRet = strCurrentFolder

   Case Else

      strRet = ""

   End Select

   NewMailSaveFolder = strRet

End Function



Private Function FindOlFolder(ByRef objRootFolder As Object, ByVal strName As String) As Outlook.Folder

Dim objFldr As Outlook.Folder

   For Each objFldr In objRootFolder.Folders

      If objFldr.Name = strName Then

         Set FindOlFolder = objFldr

         Exit For

      ElseIf objFldr.Folders.Count > 0 Then

         Set FindOlFolder = FindOlFolder(objFldr, strName)

         If Not FindOlFolder Is Nothing Then

            Exit For

         End If

      End If

   Next

End Function



Private Sub Application_ItemLoad(ByVal item As Object)

   Call Initialize_Handler

End Sub
 
Last edited by a moderator:

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
On replying to received messages I want to send from the shared mailbox that the message was addressed to.
This should work natively if you have send as permissions
Everything works as desired EXCEPT, saving the new message in the current folder.
Current folder or current mailbox sent folder?

If current folder, are you getting the correct folder?
.
I'll need to test it to see if I can repro -
 

AlphonseG

New Member
Outlook version
Outlook 365 32 bit
Email Account
Office 365 Exchange
Thank you for the reply. Sorry for the delayed response as I didn't get an email notification. I tweaked my preferences, so hopefully will get them now.
This should work natively if you have send as permissions
No, it does not work natively. I do have the permissions and regularly send via the different mailboxes.
Background - I had been using POP for many years. While the main account and each shared mailbox were separate 'accounts' in Outlook, there was only one folder tree. Even with that, I still had to use code to reply/forward using the received email address.
I just switched to Exchange connection because Microsoft is disabling basic authentication Jan 1 and won't enable MFA for POP. When I did that, Outlook created a separate folder tree for each mailbox - what a mess. After much research, I found that removing permission on the shared mailboxes via PowerShell, then adding them back, with automapping flag = false, eliminated all of the extra folder trees, leaving me with one manageable tree. I still had to use the code for reply/forward. Just had to tweak it to assign SentOnBehalfOfName instead of an 'account'.
Current folder or current mailbox sent folder?
Current folder.
If current folder, are you getting the correct folder?
Yes, I stepped through and checked. Even made sure I declared the variable as an Outlook.Folder, not just an object (had tried that initially). BTW, my version is 16.0.12624.20278 32bit.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I'll try and test you code Friday and see if I can repro problems.
 
Similar threads
Thread starter Title Forum Replies Date
B Outlook 2016 Outlook crashes when trying to print certain emails Using Outlook 5
D Outlook 2007 crashes when opening an email Using Outlook 2
O Outlook 2016 crashes at start Using Outlook 14
mrje1 Outlook 2016 Manual Email Account Setup, what is the Office 365 option etc., and Crashes Using Outlook 0
Diane Poremsky Outlook crashes when viewing HTML Messages Using Outlook 0
Diane Poremsky Outlook Crashes When You Reply or Create a New Message Using Outlook 0
J Outlook 2013 crashes saving VBA & clicking tools | digital signature Outlook VBA and Custom Forms 1
U outlook 2010 freezes/crashes when opening encrypted e-mails Using Outlook 0
T Outlook Connector crashes outlook! Outlook VBA and Custom Forms 1
M Outlook 2007 crashes with BCM BCM (Business Contact Manager) 1
e_a_g_l_e_p_i Need clarification on 2-Step Verification for Gmail using Outlook 2021 Using Outlook 9
L Opening People Outlook 2021 Using Outlook 2
e_a_g_l_e_p_i Outlook 2021 not letting me setup my Gmail using pop Using Outlook 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
P Can no longer sync Outlook with iPhone calendar after iPhone update to 17.1.1 Using Outlook 2
O Outlook - Switch from Exchange to IMAP Using Outlook 0
e_a_g_l_e_p_i Is it possible to have a reminder in Outlook 2021 for every 90 days Using Outlook 3
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
N Reply to Outlook messages by moving messages to a specific Outlook folder Outlook VBA and Custom Forms 1
O How to find out the domain and server settings that my Outlook is using? Using Outlook 2
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
H Integrating Alexa & Outlook Pro 2021 Using Outlook 2
Z Automatically adjust Outlook Reading Pane from bottom to right depending on portrait or landscape window Using Outlook 1
Rupert Dragwater Background colors not saving in Outlook 365 Using Outlook 15
petunia Outlook tasks module sunsetting? Exchange Server Administration 3
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 0
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 3
U Outlook 2021 not showing contact cards in Searches Using Outlook 1
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
H Outlook 365 O365 outlook calendar item editing Using Outlook 1
J Outlook 365 html inline images Using Outlook 0
Rupert Dragwater How to get Outlook 365 to open from websites Using Outlook 5
S Why do I have to close and reopen Outlook for macros to work? Outlook VBA and Custom Forms 2
J Outlook 2021 ScanPST errors (yet again ... sorry): repair button missing Outlook 2021 Using Outlook 0
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
K Moved pst to new computer, now Gmail not coming into Outlook Using Outlook 7
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
F Running Scripts in Outlook 2021 Using Outlook 0
Nufc1980 Outlook "Please treat this as private label" auto added to some emails - Help. Using Outlook 3
S Outlook 2019 Custom outlook Add-in using Visual Studio Outlook VBA and Custom Forms 0
V Outlook macros no longer run until VB editor is opened Outlook VBA and Custom Forms 0
R Outlook 365 How to integrate a third-party app with Outlook to track email and sms? Using Outlook 2
e_a_g_l_e_p_i I can't believe what I am seeing when trying to install Outlook 2021 Using Outlook 9
Kika Melo Outlook Calendar deleted appointments not in Deleted Items folder Using Outlook 3
P How to get a QR code for automatic signin with Outlook for iOS Using Outlook 5
J Migrating Outlook Using Outlook 1
Retired Geek Outlook for the MAC with Yahoo accounts now very broken Using Outlook 9

Similar threads

Top