Use macro to create outlook emails and attach files if there is any

Koala123

New Member
Joined
Apr 13, 2019
Messages
24
Office Version
  1. 365
Hi guys, I am trying to use the below codes to create outlook emails but seems doesn't work.

The emails I want to create don't contain any attachment, but these codes I copied from somewhere else were seems designed for emails with attachments. So if I don't have any files to attach, how can I amend these codes to make them work?

Currently I will get "Type mismatch" error when run these codes, as the attachment cells are empty.

thanks heaps guys!

Code:
Dim Info_Arr As Variant


Sub Send_Email_Macro()


Dim n%
Info_Arr = Sheet1.UsedRange
n = UBound(Info_Arr)


For i = 2 To n
  Call Send_Email_By_Outlook(i)
Next i


End Sub


Sub Send_Email_By_Outlook(ByVal i As Integer)


Recipient = Info_Arr(i, 1)
Recipientcc = Info_Arr(i, 2)
Subj = Info_Arr(i, 3)
Body = Info_Arr(i, 4)
file = Info_Arr(i, 5)
If Len(Dir(file)) = 0 Then file = ""  [CODE]
------>error


Dim objOutlook As Outlook.Application
Dim objMail As MailItem


Set objOutlook = New Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)


With objMail
.To = Recipient
.CC = Recipientcc
.Subject = Subj
.Body = Body
Dim files As Variant
files = Split(Info_Arr(i, 5), ",")
For Each file In files
.Attachments.Add file
Next
.Save
End With


Set objMail = Nothing
Set objOutlook = Nothing


End Sub
[/CODE]
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
.
If you won't be attaching files to your emails, you can delete these lines of code from the macro :

Code:
 Dim files As Variant    
    files = Split(Info_Arr(i, 5), ",")
    For Each file In files
  .Attachments.Add file  
    Next
 
Upvote 0
Let's remove the lines, try this:

Code:
Dim Info_Arr As Variant


Sub Send_Email_Macro()
    Dim n%
    Info_Arr = Sheet1.UsedRange
    n = UBound(Info_Arr)
    For i = 2 To n
      Call Send_Email_By_Outlook(i)
    Next i
End Sub


Sub Send_Email_By_Outlook(ByVal i As Integer)
    Recipient = Info_Arr(i, 1)
    Recipientcc = Info_Arr(i, 2)
    Subj = Info_Arr(i, 3)
    Body = Info_Arr(i, 4)
    
    Dim objOutlook As Outlook.Application
    Dim objMail As MailItem
    
    Set objOutlook = New Outlook.Application
    Set objMail = objOutlook.CreateItem(olMailItem)
    With objMail
      .To = Recipient
      .CC = Recipientcc
      .Subject = Subj
      .Body = Body
      .Save
    End With
    Set objMail = Nothing
    Set objOutlook = Nothing
End Sub
 
Upvote 0
Hi Dante thanks a lot, this works, at the time I am just wondering is it possible to fix that mismatch error? As in some occasions I may need to attach some files
 
Upvote 0
.
Try this to replace the existing :

Code:
files = Split(Info_Arr(i, 5), ",")
    For Each file In files
		If file <> 0 then
  			.Attachments.Add file  
		End If
    Next

or

Code:
files = Split(Info_Arr(i, 5), ",")
    For Each file In files
		If file <> "" then
  			.Attachments.Add file  
		End If
    Next
 
Upvote 0
Try this:

Code:
Dim Info_Arr As Variant


Sub Send_Email_Macro()
    Dim n%
    Info_Arr = Sheet1.UsedRange
    n = UBound(Info_Arr)
    For i = 2 To n
      Call Send_Email_By_Outlook(i)
    Next i
End Sub


Sub Send_Email_By_Outlook(ByVal i As Integer)
    Recipient = Info_Arr(i, 1)
    Recipientcc = Info_Arr(i, 2)
    Subj = Info_Arr(i, 3)
    Body = Info_Arr(i, 4)
[COLOR=#0000ff]    file = Info_Arr(i, 5)[/COLOR]
    
    Dim objOutlook As Outlook.Application
    Dim objMail As MailItem
    
    Set objOutlook = New Outlook.Application
    Set objMail = objOutlook.CreateItem(olMailItem)
    With objMail
      .To = Recipient
      .CC = Recipientcc
      .Subject = Subj
      .Body = Body
[COLOR=#0000ff]      If file <> "" Then[/COLOR]
[COLOR=#0000ff]        Dim files As Variant, f As Variant[/COLOR]
[COLOR=#0000ff]        files = Split(Info_Arr(i, 5), ",")[/COLOR]
[COLOR=#0000ff]        For Each f In files[/COLOR]
[COLOR=#0000ff]            If Dir(f) <> "" Then[/COLOR]
[COLOR=#0000ff]                .Attachments.Add file[/COLOR]
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        Next[/COLOR]
[COLOR=#0000ff]      End If[/COLOR]
      .Save
    End With
    Set objMail = Nothing
    Set objOutlook = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,701
Messages
6,173,920
Members
452,539
Latest member
deeme

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