Macro to open a new email via Outlook

BartGoem

New Member
Joined
Jan 31, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a macro to send a new email via outlook.

The problem is I have multiple accounts in my outlook and I one to select one of the accounts to send the email.

Any idea how I can solve this?

Txs!

This is my VBA code so far:

Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem
Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)


With olEmail
.BodyFormat = olFormatHTML
.Display

.CC = ""
.Subject = "New email"

End With
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,

Thanks for your reply.

I managed to solve it by making the code like this:

Private Sub Knop397_Click()


Dim olApp As outlook.Application
Dim olEmail As outlook.MailItem
Set olApp = New outlook.Application

Dim oAccount As outlook.Account

For Each oAccount In outlook.Session.Accounts
If oAccount = "info@xxxxxxx.com" Then ' account name i
Set olEmail = outlook.CreateItem(olMailItem)
olEmail.SendUsingAccount = oAccount
Exit For
End If
Next

With olEmail
.BodyFormat = olFormatHTML
.Display

'.To = "enter email addresses here"
.CC = ""
.Subject = "xxxxxxx!"

End With
End Sub

I need the email to "insert as text" a html file. Anybody know how to do this?

Txs!

Bart
 
Upvote 0
try this

VBA Code:
Private Sub SendEmail(fromAddess As String, toAddess As String, subject As String, body As String)
    Dim app As Object
    Dim item As Object

    On Error GoTo eh

    Set app = CreateObject("Outlook.Application")
    Set item = app.CreateItem(0)

    With item
        .To = toAddess
        .subject = subject
        .HTMLBody = "<html><body>" & body & "</body></html>"
        .Send
    End With

    GoTo out

eh:
    MsgBox "An error occurred: " & Err.Description

out:
    Set app = Nothing
    Set item = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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