Excel VBA to send outlook email - Modify Subject line & Fix attachments

Miratshah

Board Regular
Joined
Nov 29, 2016
Messages
57
Hi Guys,

So I am a complete novice in Excel VBA. I want to send email using Excel VBA.

I have excel sheet in following format
Column A - Name
Column B - To
Column C - Subject
Column D - Path of attachment

Now, each row of this excel should open a new email. I have modified a code to suit me requirements except, I have two issues for which need help.

1. Subject line should be picked up from Column C (similar to Name & To field)

2. Need to fix the range for selecting attachment. As of now, every email has all the attachments from column D. I need only attachment as mentioned in that particular row for each email.

Hope some one can help.

Attaching my code below -

HTML:
Sub Send_Files()'Working in Excel 2000-2016'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm    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 strbody As String

    With Application        .EnableEvents = False        .ScreenUpdating = False    End With
    Set sh = Sheets("Sheet1")
    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
        'Enter the path/file names in the E:E column in each row        Set rng = sh.Cells(cell.Row, 1).Range("D1:D1")
        If cell.Value Like "?*@?*.?*" And _           Application.WorksheetFunction.CountA(rng) > 0 Then            Set OutMail = OutApp.CreateItem(0)                        strbody = "This is the body"                                                            With OutMail            Set .SendUsingAccount = OutApp.Session.Accounts.Item(2)            .Display                .to = cell.Value                .Subject = "TestMail"                .HTMLBody = strbody & .HTMLBody                              
                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)                    If Trim(FileCell) <> "" Then                        If Dir(FileCell.Value) <> "" Then                            .Attachments.Add FileCell.Value                        End If                    End If                Next FileCell
                .Display  'Or use .send            End With
            Set OutMail = Nothing        End If            Next cell
    Set OutApp = Nothing    With Application        .EnableEvents = True        .ScreenUpdating = True    End WithEnd Sub

Not sure why the code looks non-formatted.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this

Code:
Sub Send_Files() 'Working in Excel 2000-2016'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
  Dim OutApp As Object, OutMail As Object, sh As Worksheet
  Dim cell As Range, FileCell As Range, rng As Range, strbody As String


  'Enter the path/file names in the D:D column in each row
  Application.EnableEvents = False
  Application.ScreenUpdating = False
  Set sh = Sheets("Sheet1")
  Set OutApp = CreateObject("Outlook.Application")
  For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
[COLOR=#0000ff]    Set FileCell = Cells(cell.Row, "D")[/COLOR]
    If cell.Value Like "?*@?*.?*" And FileCell.Value <> "" Then
      Set OutMail = OutApp.CreateItem(0)
      strbody = "This is the body"
      With OutMail
        Set .SendUsingAccount = OutApp.Session.Accounts.Item(2)
        .to = cell.Value
[COLOR=#0000ff]        .Subject = Cells(cell.Row, "C").Value[/COLOR]
        .HTMLBody = strbody & .HTMLBody
[COLOR=#0000ff]        If Dir(FileCell.Value) <> "" Then .Attachments.Add FileCell.Value[/COLOR]
        .Display  'Or use .send
      End With
      Set OutMail = Nothing
    End If
  Next cell
  Set OutApp = Nothing
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,655
Latest member
goranzoric

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