Select account to send email from

Frankietheflyer

New Member
Joined
Nov 17, 2017
Messages
30
Hi

I have a code that runs through a list of people and sends individual files to each one. The code automatically sends the emails from one of two email accounts I have. Problem is, it's the account that isn't associated with the message!

As this spreadsheet will be used by many different people I can't "hard wire" it to the correct account s
o I need to be able to select which account is used to send the email out under.

I think there are probably two ways of doing this:

1.If there is more than one account when I start to run the code it asks me to type in the account address I want to send the emails from and uses that one.

2. The code automatically finds all the accounts I have in outlook (one's a "live.com.au" and the other is "y7mail.com") and lets me select the one I want to use.

It also needs to recognise if someone only has one email account and just carry on.

Anyone got a code that can do either of these?

My current code for sending the emails is as follows
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Private Sub MailSheets_Click()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range
    Dim File As Variant
    Dim myFile As Variant, FolderName As String
    Dim strPath As String, rngWB As Range, wsRO As Worksheet
    
[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Set sh = Sheets("Contacts")[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Set OutApp = CreateObject("Outlook.Application")[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]     For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]        'Enter the path/file names in the C:C column in each row
        
        Set rng = sh.Cells(cell.Row, 1).Range("C1:C100")
        
File = ("C:\Users\" & Environ("USERNAME") & ("\Documents\PDF Completed Sheets for Team Captains ") & ThisWorkbook.Sheets("Running Order").Range("F1").Value & ("\") & cell.Offset(0, 1) & ".pdf")[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]            With OutMail
                .to = cell.Value
                .Subject = cell.Offset(0, 4) & " Team Sheet"
                .Body = "Hi " & cell.Offset(0, 2).Value & Chr(13) & Chr(13) & "Please find the Competed Team Sheet for " & cell.Offset(0, 4) & " attached." _
                        & Chr$(13) & Chr$(13) & "Trust you had fun!"[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]                   .Attachments.Add File
                            
                 '.Display    'Or use
                 .Send
            End With[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]            Set OutMail = Nothing
        End If
    Next cell[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]MsgBox "Emails have been sent"
End Sub[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif][/FONT]

Thanks everyone!

Frankie
 
Try this

Change "y7mail.com" to the email you want to send

Code:
Private Sub MailSheets_Click()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range
    Dim File As Variant
    Dim myFile As Variant, FolderName As String
    Dim strPath As String, rngWB As Range, wsRO As Worksheet
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    Set sh = Sheets("Contacts")
    Set OutApp = CreateObject("Outlook.Application")
     For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
        'Enter the path/file names in the C:C column in each row
        
        Set rng = sh.Cells(cell.Row, 1).Range("C1:C100")
        
File = ("C:\Users\" & Environ("USERNAME") & ("\Documents\PDF Completed Sheets for Team Captains ") & ThisWorkbook.Sheets("Running Order").Range("F1").Value & ("\") & cell.Offset(0, 1) & ".pdf")




        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)
            With OutMail
                '.SendUsingAccount = OutMail.Session.Accounts.Item(1)
               [COLOR=#0000ff] .[FONT=Roboto]SentOnBehalfOfName = [/FONT][/COLOR][COLOR=#212121][FONT=Roboto]"[/FONT][/COLOR][COLOR=#ff0000]y7mail.com[/COLOR][COLOR=#212121][FONT=Roboto]"[/FONT][/COLOR]
                .to = cell.Value
                .Subject = cell.Offset(0, 4) & " Team Sheet"
                .Body = "Hi " & cell.Offset(0, 2).Value & Chr(13) & Chr(13) & "Please find the Competed Team Sheet for " & cell.Offset(0, 4) & " attached." _
                        & Chr$(13) & Chr$(13) & "Trust you had fun!"
                   .Attachments.Add File
                            
                 '.Display    'Or use
                 .Send
            End With
            Set OutMail = Nothing
        End If
    Next cell
    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
MsgBox "Emails have been sent"
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Code:
[COLOR=#0000ff].[FONT=Roboto]SentOnBehalfOfName = [/FONT][/COLOR][COLOR=#212121][FONT=Roboto]"[/FONT][/COLOR][COLOR=#ff0000]y7mail.com[/COLOR][COLOR=#212121][FONT=Roboto]"[/FONT][/COLOR]

Nice one Dante :)
 
Upvote 0
Hi Guys

Really appreciate the responses. I see how this works, but as I said previously the workbook will be used by many different people. I can't expect everyone to go into the code and change the "send on behalf of" detail to their emaill provider as most of the users will be basic Excel users. Some of the users will have a private email address and a second one dedicated to email traffic relative to the sport. They would want to send the information coming out of this workbook via the sports email, not their private one. Therefore I have a bit of code that finds out if the user has more than one email, allows them to select which one they want to use and then write that address in cell L1. The mail send code needs to automatically use the selected address in L1 as the "send from" address.
I tried several things but maybe I need to try .SendOnBehalfOfName = sh.Range("L1"). (sh is the DIM for this worksheet)
 
Upvote 0
I understand your concern so that users do not enter to modify the code.


What I am trying to do is make a test, so you can see with which instruction the email account can change.


Once you are with which instruction you can change the email account, then you can direct it to a cell.
 
Upvote 0
Got it!!! Apologies, I mis-understood the aim of the exercise!

OK. So the first run produced a Run-Time error - "'-2147221233 (800-40 10f)': The operation failed. The messaging interfaces have returned an unknown error. If the problem persists, restart Outlook. The Operation failed. An object cannot be found. (Sorry, I can't seem to post a screen shot!).

I then changed "y7mail.com" to the full email address (myemail@y7mail.com). The code returned the message that the emails had been sent but in my prime email (The one I don't want to send from) I got a Systems Administrator message saying

Your message did not reach some or all of the intended recipients.

Subject: xxxxxx Team Sheet
Sent: 13/11/2019 11:27 AM

The following recipient(s) cannot be reached:

'myemail@y7mail.com' on 13/11/2019 11:28 AM
This message could not be sent. You do not have the permission to send the message on behalf of the specified user.

This came up for all three test emails that I was trying to send from the list.

Frankie
 
Upvote 0
Update time!

I edited the code with .SentOnBehalfOfName = sh.Range("L1"), selected the y7mail.com email in L1 and it works in so far as the code completes it's run through. However, as mentioned above I get the Administration message that the message did not reach some of the intended recipients from the live.com.au email that shouldn't be sending anything!

If I select the live.com.au address into L1 the code sends the emails no problem.
 
Last edited:
Upvote 0
I guess it's a security issue for email accounts. I hope someone else can help you with the problem. Those are the instructions I know.
 
Upvote 0
Yes. I searched through the internet and found a couple of hints on how to get around the issue. Turns out that you can only delegate access to addresses in the offline contacts list. My list only show the live.com.au address. There was some direction as to how to add an address to the offline list, but my computer won't allow me to do it(!!!). It may be an external war between Microsoft emails and Yahoo that has Microsoft blocking Yahoo addresses.
So, I admit defeat with this one!

Thanks for your assistance and patience DanteAmor and Paul Ked. I've still learnt a few things about Coding that will come in useful. Much appreciated.

Frankie.
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,980
Members
452,540
Latest member
haasro02

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