Outlook email using VBA

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Hi all,

I am current in the process of trying to automatically send emails via outlook from data found in my excel spreadsheet. Here's what I've done so far;

Code:
Sub Send_email()

   Dim sh As Worksheet
   Set sh = ThisWorkbook.Sheets("email")
    
   Dim Outlook As Object
   Dim msg As Object
    
   Set Outlook = CreateObject("outlook.application")
   
   Dim i As Integer
   Dim last_row As Integer
    
   last_row = Application.WorksheetFunction.CountA(sh.Range("B:B"))
    
   For i = 2 To last_row
   Set msg = Outlook.createitem(0)
    If sh.Range("E" & i) = "" Then
    
   msg.To = sh.Range("B" & i).Value
   msg.bcc = sh.Range("C" & i).Value
   msg.Subject = "Request Status"
   msg.body = "Your request has been"
    
   msg.display
   sh.Range("E" & i).Value = "sent"
    Else
    sh.Range("E" & i) = "Sent"
    End If
    
Next i


MsgBox "Email sent"


End Sub

The above script is working perfectly but im looking to compose the email a bit more in depth. See https://imgur.com/Ko2gRRM for a visual of the table i am currently using for my data

Ko2gRRM
Ko2gRRM


Code:
  msg.body = "Your request has been"

I'm looking to reference call D that currently shows the status to appear on the end of the current text (Script above). So the email will read "Your request has been Approved" for example.

Hope this makes sense and would appreciate any help on this.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this

Hi all,

I am current in the process of trying to automatically send emails via outlook from data found in my excel spreadsheet. Here's what I've done so far;

Code:
Sub Send_email()

   Dim sh As Worksheet
   Set sh = ThisWorkbook.Sheets("email")
    
   Dim Outlook As Object
   Dim msg As Object
    
   Set Outlook = CreateObject("outlook.application")
   
   Dim i As Integer
   Dim last_row As Integer
    
   last_row = Application.WorksheetFunction.CountA(sh.Range("B:B"))
    
   For i = 2 To last_row
     Set msg = Outlook.createitem(0)
    If sh.Range("E" & i) = "" Then
    
      msg.To = sh.Range("B" & i).Value
      msg.bcc = sh.Range("C" & i).Value
      msg.Subject = "Request Status"
      msg.body =[COLOR=#ff0000] "Your request has been " & sh.Range("D" & i).value[/COLOR]
    
      msg.display
      sh.Range("E" & i).Value = "sent"
   Else
      sh.Range("E" & i) = "Sent"
   End If    
   Next i
  MsgBox "Email sent"
End Sub

The above script is working perfectly but im looking to compose the email a bit more in depth. See https://imgur.com/Ko2gRRM for a visual of the table i am currently using for my data

Ko2gRRM
Ko2gRRM


Code:
  msg.body = "Your request has been"

I'm looking to reference call D that currently shows the status to appear on the end of the current text (Script above). So the email will read "Your request has been Approved" for example.

Hope this makes sense and would appreciate any help on this.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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