Vba send email

Here is a basic example of how to send an email using VBA in Excel:

Step 1: Enable the Outlook Object Library

In the Visual Basic Editor (VBE), go to Tools > References and check the box next to "Microsoft Outlook XX.X Object Library" (where XX.X is the version of Outlook you have installed).

Step 2: Create a new module

In the VBE, go to Insert > Module to create a new module.

Step 3: Write the code

Paste the following code into the new module:

Sub SendEmail()
    Dim olApp As Object
    Dim olMail As Object

    ' Create a new instance of Outlook
    Set olApp = CreateObject("Outlook.Application")

    ' Create a new email
    Set olMail = olApp.CreateItem(0) ' 0 = olMailItem

    ' Set the email properties
    With olMail
       .To = "[email protected]"
       .Subject = "Test Email"
       .Body = "This is a test email sent from Excel using VBA."
       .Send
    End With

    ' Clean up
    Set olMail = Nothing
    Set olApp = Nothing
End Sub

Step 4: Run the code

To run the code, click the "Run" button or press F5. The email will be sent to the recipient specified in the code.

Tips and Variations

Error Handling

Security Considerations