Create email with same body but different addresses (using drop down menu)

jmcconnell

New Member
Joined
Feb 2, 2019
Messages
35
So I've got a drop down menu which creates an email. Body of the email is always the same but the addresses and subject change depending on the option chosen from the drop down list. It pulls the email addresses from a table and also inserts the contents of the clipboard to the body of the email.

The only way I can get it to populate with different email addresses is to keep repeating the full section of code below and change the cell location in the .To &.From sections. Is there a way to streamline this:


Private Sub Cark()


Dim OutApp As Object
Dim OutMail As Object
Dim olInsp As Object
Dim wdDoc As Object
Dim oRng As Object


On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If Err <> 0 Then Set OutApp = CreateObject("Outlook.Application")
On Error GoTo 0


Set OutMail = OutApp.CreateItem(0)


With OutMail
.BodyFormat = 2
'Email addresses pulled from spreadsheet
.To = Sheets("emails").Range("A1")
.CC = Sheets("emails").Range("C1")
.Subject = "Cark fault"


Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)


'Body of email
If Time < TimeValue("12:00:00") Then
oRng.Text = "Good Morning," & vbCr & vbCr & _
"Please see the fault below:" & vbCr & vbCr
ElseIf Time > TimeValue("12:00:00") And Time < TimeValue("17:00:00") Then
oRng.Text = "Good Afternoon," & vbCr & vbCr & _
"Please see the fault below:" & vbCr & vbCr
Else
oRng.Text = "Good Evening," & vbNewLine & vbNewLine & _
"Please see the fault below:" & vbCr & vbCr
End If
'Insert clipboard contents
oRng.collapse 0
oRng.Paste
.display
End With
'Tidyup
Set OutApp = Nothing
Set OutMail = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
End Sub

So can I create a sub that contains the body of the email separate to the email address section?

Thank you.
Kind regards,
James.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
.
Haven't tested for all times of the day ... but I believe this should work. Or at least give you an idea which direction to go ...

Code:
Option Explicit


Sub Cark()




Dim OutApp As Object
Dim OutMail As Object
Dim olInsp As Object
Dim wdDoc As Object
Dim oRng As Object




On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If Err <> 0 Then Set OutApp = CreateObject("Outlook.Application")
On Error GoTo 0




Set OutMail = OutApp.CreateItem(0)




With OutMail
    .BodyFormat = 2
    'Email addresses pulled from spreadsheet
    '.To = Sheets("emails").Range("A1")
    '.CC = Sheets("emails").Range("C1")
    
    
    
    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor
    Set oRng = wdDoc.Range(0, 0)
    
    
    'Body of email
    If Time < TimeValue("12:00:00") Then
    
        .body = "Good Morning," & vbCr & vbCr & _
        "Please see the fault below:" & vbCr & vbCr
        .To = Sheets("emails").Range("A1").Value
        .CC = Sheets("emails").Range("C1").Value
        
    ElseIf Time > TimeValue("12:00:00") And Time < TimeValue("17:00:00") Then
    
        .body = "Good Afternoon," & vbCr & vbCr & _
        "Please see the fault below:" & vbCr & vbCr
        .To = Sheets("emails").Range("A2").Value
        .CC = Sheets("emails").Range("C2").Value
        
    Else
    
        .body = "Good Evening," & vbNewLine & vbNewLine & _
        "Please see the fault below:" & vbCr & vbCr
        .To = Sheets("emails").Range("A3").Value
        .CC = Sheets("emails").Range("C3").Value
        
    End If
    
    .Subject = "Cark fault"
    
    'Insert clipboard contents
    oRng.collapse 0
    oRng.Paste
    .display
    
End With


'Tidyup
Set OutApp = Nothing
Set OutMail = Nothing
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing


End Sub
 
Upvote 0
Hi,

You need to access whatever kind of dropdown selection you are using.

E.g if it were a Combo box:

Code:
.To = Worksheets("Sheet1").ComboBox1.Value

Same for the .Subject.
 
Upvote 0
Thanks Dave & Logit for the assistance.

Sorry, I never explained it very well. So i've got a drop down menu where I select an option (in the code above its 'Cark') I then press a button which generates the email as per my code. However if I select a different option from the menu and press the button it generates the same email but the addresses are different.

However, the only way I've managed this is to repeat the same section of code above over and over for every option on my combo box list and just keep changing the .To and .From.

I was hoping there was another way around this?

Thanks again!!
 
Upvote 0
.
I still believe my code will accomplish the goal unless the emails should not change based on the time of day.
Are you saying disregard the time of day altogether ?
 
Upvote 0
Well I thought I understood but now I'm lost.

Is what you are saying is that you generate the same email and addresses/subject for every option and you want to use different email addresses and subject depending on the selected option?

If so it sounds like you could use 'Select Case' to change those parameters in the email at generation time dependent on the option selected.

If not :eeek:

Logit - it's all yours!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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