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.
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.