Error in VBA-code for send mail with Attached file using outlook.

KlausW

Active Member
Joined
Sep 9, 2020
Messages
381
Office Version
  1. 2016
Platform
  1. Windows
Hi, I use this VBA code to send mail with Attached file using outlook. But I for this error message see the attachment, someone who can help.
Any help will be appreciated.
Best regards Klaus W

VBA Code:
Option Explicit

Private Sub Send_Email_to_an_Address_in_Cell()

Dim MyOutlook As Object
Set MyOutlook = CreateObject("Outlook.Application")

Dim MyMail As Object

Set MyMail = MyOutlook.CreateItem(olMailItem)

MyMail.To = Range("a17").Value '& "; " & Range("a18").Value & "; " & Range("a19").Value & "; " & Range("a20").Value

MyMail.Subject = Range("a24").Value
MyMail.Body = Range("a25").Value & vbNewLine & vbNewLine & Range("a26").Value & vbNewLine & _
        Range("a27").Value & vbNewLine & Range("a28").Value & vbNewLine & Range("a28").Value & vbNewLine & _
        Range("a29").Value & vbNewLine & _
        Range("a30").Value

Attached_File = Range("a31").Value
MyMail.Attachments.Add Attached_File

MyMail.Send

End Sub
 

Attachments

  • 2023-05-18 (1).png
    2023-05-18 (1).png
    123.7 KB · Views: 15

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi there...

What happens if you add this line to the beginning...

VBA Code:
Dim Attached_File As Outlook.Attachments
 
Upvote 0
Rory is correct... Only realised now that the Attached file is referring to a string in a range...
 
Upvote 0
Hi RoryA
Thanks allot its works as it shoud. Have a great day,
Best regards from Denmark. Klaus W
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,146
Members
449,098
Latest member
Doanvanhieu

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