Excel VBA to compose an email in Outlook

MisterProzilla

Active Member
Joined
Nov 12, 2015
Messages
264
Hi there,

This one's probably as basic as it gets for some users, but I've tried a bunch of how-to guides and can't get it working.

Please see code below; the comment lines should explain what I'm trying to do. The problem is it's coming up with an error "Compile error: Assignment to constant not permitted" and it's highlighting the 'olMail' text that I've put in bold below. No idea what that means, unfortunately :(

Any help appreciated!

Code:
Sub Remind_Manager()

' Button to email reminder of upcoming review meeting
' Administrator clicks in the staff's row, then clicks button to send a reminder
' column L contains the line manager's name, retrieved by a VLOOKUP

strEmailTo = "L" & ActiveCell.Row


Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon
Set [B]olMail[/B] = olApp.CreateItem(olMailItem)
olMail.To = strEmailTo


olMail.Subject = "Test"
olMail.Body = vbCrLf & "Hi, this is a test" & strEmailTo & vbCrLf & vbCrLf & "Regards"

'
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi

I have used this routine successfully in many different codes. See if you can adapt it to your situation. This code will work even if Outlook is closed, however i do not need to enter a password to open Outlook.

Code:
Sub eml()


    Dim objOutlook As Object
    Dim objMail As Object


    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(0)


    With objMail
        .to = Range("B3").Value
        .cc = Range("C3").Value
        .bcc = Range("D3").Value
        .Subject = "Fund " & MonthName(Month(Date) - 1, False) & " " & Format(Date, "YYYY")
        .Body = Range("D3").Value
        '.Attachments.Add rngAttach.Value
        .Display                   'Instead of .Display, you can use .Send to send the email _
                                         or .Save to save a copy in the drafts folder
    End With


    Set objOutlook = Nothing
    Set objMail = Nothing
    'Set rngTo = Nothing
    'Set rngSubject = Nothing
    'Set rngBody = Nothing
    'Set rngAttach = Nothing


End Sub

HTH

igold
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

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