Excel VBA save emails the draft folder of the SentOnBehalfOfName account

Bering

Board Regular
Joined
Aug 22, 2018
Messages
185
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I would like to add a minor change to the below code. Currently the automatic emails get saved in my personal account in the draft folder. However, I would like them to be saved in the draft folder of the
.SentOnBehalfOfName = "abc@cde.com" account.

I thing I should use .move, but despite several attempts, I have not figure out yet how I should refer to this specific account/folder.

Hope you guys can help me. Thank you

VBA Code:
Sub Send_Email_With_Signature()

    Dim objOutApp As Object, objOutMail As Object
    Dim strBody As String, strSig As String
    Dim strLocation, strFileName, strFileExt, pass As String
    Dim StrSignature As String, sPath As String


  
    Set objOutApp = CreateObject("Outlook.Application")
    Set objOutMail = objOutApp.CreateItem(0)
  
    On Error Resume Next
  
    With objOutMail
  
  
        'SET THE EMAIL CONDITIONS
        .To = ActiveSheet.Range("MailDestinataries").Value
        .CC = ActiveSheet.Range("CCMailDestinataries").Value
        .BCC = ""
        .Subject = ActiveSheet.Range("MailSubject").Value
      
        'ADD ATTACHMENTS
         strLocation = ActiveSheet.Range("AttachPath").Value
         strFileName = ActiveSheet.Range("AttachFileName").Value
         strFileExt = ActiveSheet.Range("AttachFileExt").Value
'
        .Attachments.Add strLocation & strFileName & strFileExt
      
      
        'IF SENT FROM ANOTHER EMAIL ACCOUNT (MUST ALREADY BE SETUP)
        .SentOnBehalfOfName = "abc@cde.com"
      
        'CHECK NAMES, ENSURES INTERNAL EMAIL ADDRESSES EXISTS IN ADDRESS BOOK
        .Recipients.ResolveAll
        .Display


        'GET THE HTML CODE FROM THE SIGNATURE
        strSig = .HTMLbody
      
        'CONVERT BODY IN HTML
      
        ActiveSheet.Range("MailBody").Copy
        ActiveSheet.Range("G9").PasteSpecial Paste:=xlPasteValues
        ActiveSheet.Range("H9") = "=fnConvert2HTML(RC[-1])"
      
        strBody = ActiveSheet.Range("H9")
        strBody = "<font style=""font-family: Raleway; font-size: 11pt;""/font>" & strBody
      
        'COMBINE THE EMAIL WITH THE SIGNATURE
      
        .HTMLbody = strBody & vbNewLine & vbNewLine & strSig
      
        'AUTOMATICALLY SAVE EMAIL AS DRAFT (IT WILL STILL BRIEFLY POPUP)
        .Display
        .Save
        .Close 0
        ActiveSheet.Range("G9,H9").ClearContents
  
    End With
  
    On Error GoTo 0
    Set objOutMail = Nothing
    Set objOutApp = Nothing

End Sub
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hello,

I fixed it.

Thanks

VBA Code:
Sub Send_Email_With_Signature()
 
    Dim objOutApp As Object, objOutMail As Object
    Dim strBody As String, strSig As String
    Dim strLocation, strFileName, strFileExt, pass As String
    Dim StrSignature As String, sPath As String
    Dim objDestFolder As Outlook.MAPIFolder
    Dim objNS As Outlook.Namespace
    
    Set objOutApp = CreateObject("Outlook.Application")
    Set objOutMail = objOutApp.CreateItem(0)
    Set objNS = objOutApp.GetNamespace("MAPI")
    
'    On Error Resume Next
    
    With objOutMail
    
    
        'SET THE EMAIL CONDITIONS
        .To = ActiveSheet.Range("MailDestinataries").Value
        .CC = ActiveSheet.Range("CCMailDestinataries").Value
        .BCC = ""
        .Subject = ActiveSheet.Range("MailSubject").Value
        
        'ADD ATTACHMENTS
         strLocation = ActiveSheet.Range("AttachPath").Value
         strFileName = ActiveSheet.Range("AttachFileName").Value
         strFileExt = ActiveSheet.Range("AttachFileExt").Value
'
        .Attachments.Add strLocation & strFileName & strFileExt
        
        
        'IF SENT FROM ANOTHER EMAIL ACCOUNT (MUST ALREADY BE SETUP)
        .SentOnBehalfOfName = "abc@cde.com"
        
        'CHECK NAMES, ENSURES INTERNAL EMAIL ADDRESSES EXISTS IN ADDRESS BOOK
        .Recipients.ResolveAll
        .Display


        'GET THE HTML CODE FROM THE SIGNATURE
        strSig = .HTMLbody
        
        'CONVERT BODY IN HTML
        
        ActiveSheet.Range("MailBody").Copy
        ActiveSheet.Range("G9").PasteSpecial Paste:=xlPasteValues
        ActiveSheet.Range("H9") = "=fnConvert2HTML(RC[-1])"
        
        strBody = ActiveSheet.Range("H9")
        
       
        strBody = "<font style=""font-family: Raleway; font-size: 11pt;""/font>" & strBody
        
        'COMBINE THE EMAIL WITH THE SIGNATURE
        
        .HTMLbody = strBody & vbNewLine & vbNewLine & strSig
        
        'AUTOMATICALLY SAVE EMAIL AS DRAFT (IT WILL STILL BRIEFLY POPUP)
        .Display
        .Save
        .Close 0
               
        ' Set the destination folders
            Set objDestFolder = objNS.Folders("abc@cde.com").Folders("Drafts")
        
         objOutMail.Move objDestFolder
 
        ActiveSheet.Range("G9,H9").ClearContents
    
    End With
    
    On Error GoTo 0
    Set objOutMail = Nothing
    Set objOutApp = Nothing

End Sub
 
Upvote 0
Solution
However, I receive a run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found." at Set objDestFolder = objNS.Folders("abc@cde.com").Folders("Drafts"), may I know how to solve?
 
Upvote 0

Forum statistics

Threads
1,215,736
Messages
6,126,552
Members
449,318
Latest member
Son Raphon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top