How to create a mailitem? - vba

I'm trying to send the active Excel workbook as an attachment via Outlook.
Whenever I run the code it says
Invalid use of New key word
at New Outlook.MailItem`.
Sub SendOutlook()
'Declaring Variables
Dim OutlookApp As Outlook.Application
Dim OutlookEmail As Outlook.MailItem
'Assigning variables to create outlook application and mailitem
Set OutlookApp = New Outlook.Application
Set OutlookEmail = New Outlook.MailItem
With OutlookEmail
'Format of the mail
.BodyFormat = olFormatPlain
'Body of the mail
.Body = "Dear Someone" & vbNewLine & "How are you?"
'To whom you want to send mail
.To = "Someone#somewhere.com"
'Subject of mail
.Subject = "Write Subject Here"
'TO add an attachment
.Attachments.Add ActiveWorkbook.FullName
'sends the mail
.Send
End With
End Sub

You cannot create a MailItem via New. It must be created using CreateItem of the the Outlook Application Object.
Set OutlookApp = New Outlook.Application
Set OutlookEmail = OutlookApp.CreateItem(olMailItem)

As far as I got to know from the research is that Programmatic access to sending emails is a security risk, so it's not allowed via VBA.

You can use a programmatic approach with the following:
Option Explicit
Private outlook_app As Object
Private outlook_mailItem As Variant
Sub send_email()
Set outlook_app = CreateObject("Outlook.Application")
With outlook_app.CreateItem(outlook_mailItem)
.To = "Someone#somewhere.com"
.Subject = "Write Subject Here"
.Body = "Dear Someone" & vbNewLine & "How are you?"
.send
End With
Set outlook_app = Nothing
End Sub

Related

Convert incoming mail to plain text

I want to convert incoming HTML mail in Outlook to "plain text" and forward the e-mail.
I tried several examples of code.
Sub ConvertToPlain(MyMail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
objMail.BodyFormat = olFormatPlain
objMail.Save
Set objMail = Nothing
End Sub
You can just create a new mail-item, and set the .body property. Reading the .body of a mail-item only gets the text, without any formatting (as opposed to reading the .HTMLBody, which gets the full HTML).
Here's an example of a sub that'll send the unformatted text of an email to whatever addresses you specify
Sub sendPlainText(MyMail As MailItem, sendTo As String)
Dim newMail As Outlook.MailItem
Set newMail = Application.CreateItem(olMailItem) 'Create a new email
With newMail
.To = sendTo 'Whoever you want to send the new mail item to
.subject = MyMail.subject 'Copy subject of original email
.Body = MyMail.Body 'Copy plain text of body to new mail item
.send 'Send the new email
End With
End Sub

Outlook vba - How can i add suffix on mail subject before forward, reply or replay to all?

I need to forward or reply to a mail, then add suffix on subject, and send it.
thanks in advance
You could reply or replyAll an email and add suffix to subject using the below code:
Option Explicit
Sub ReplyMSG()
Dim olItem As Outlook.MailItem
Dim olReply As MailItem ' Reply
Dim olRecip As Recipient ' Add Recipient
For Each olItem In Application.ActiveExplorer.Selection
Set olReply = olItem.ReplyAll
olReplay.Subject = = olItem.Subject & “suffix”
Set olRecip = olReply.Recipients.Add("Email Address Here") ' Recipient Address
olRecip.Type = olCC
olReply.HTMLBody = "Hello, Thank you. " & vbCrLf & olReply.HTMLBody
olReply.Display
'olReply.Send
Next olItem
End Sub
Reference from:
Outlook Reply or ReplyAll to an Email
If you want to forward an email and add suffix to subject, please refer to the below code:
Sub ForwardEmail(item As Outlook.MailItem)
Dim oMail As MailItem
On Error GoTo Release
If item.Class = olMail Then
Set oMail = item.Forward
oMail.Subject = oMail.Subject & “suffix”
oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
oMail.Recipients.Add "email address here"
oMail.Save
oMail.Send
End If
Release:
Set oMail = Nothing
Set oExplorer = Nothing
End Sub
Reference from:
Forward Email with its attachment in Outlook 2010
Properly going to be hard, because depending on your hierarchy location you would only have a set number of rights and i do not think VB Macro would be allowed i know in many organizations here in Denmark VB Macros is a no no.
But you can create a Macro in the background where you grep Current mail and then alter the subject and then set it to when you press CTRL + R as you normally do when you reply but it might conflict so be careful because you might not want it on all emails. that you reply to.

Macro doesn't show open mail

Below is my macro to send an email but it does not open a new email .
This macro is in the outlook rules.
Have you any ideas?
Sub sendemail()
Dim ns As NameSpace
'Dim newMail As Outlook.MailItem
Set ns = GetNamespace("MAPI")
Dim newMail As MailItem
Set newMail = Application.CreateItem()
With newMail
.To = "aaa#bbb" <--adress to whitch I want to send an email
.Subject = "test"
.Display
End With
Set newMail = Nothing
End Sub
first you need to add up the outlook reference:
There you go:
then:
Sub sendemail()
Dim outlook As New outlook.Application
Dim newMail As outlook.MailItem
Set newMail = outlook.CreateItem(olMailItem)
With newMail
.To = "email#address.com" ' <--adress to whitch I want to send an email
.Subject = "test from Steph"
.Display
.Send
End With
Set newMail = Nothing
outlook.Quit
Set outlook = Nothing
End Sub
Argument must be type MailItem or MeetingItem for the subroutine to be available in Outlook Rules Wizard.
Try this.
Option Explicit
Sub SendEmail(NewMail As Outlook.MailItem)
Set NewMail = Application.CreateItem(olMailItem)
With NewMail
.To = "aaa#bbb" '<--adress to whitch I want to send an email
.Subject = "test"
.Display
End With
Set NewMail = Nothing
End Sub
Youre not specifying the item to create. See here:
Private Sub SendEmail(ByVal workcenter As Integer, ByVal time As Date)
Dim objApp As Object
Dim objEmail As Object
Set objApp = CreateObject("Outlook.Application")
Set objEmail = CreateObject("Outlook.MailItem")
With objEmail
.To = "emailexampe#website.com"
.Subject = "Multiple Shop Orders run for line " & workcenter & " at " & time
.body = "TEST"
.display
End With
Set objEmail = Nothing
Set objApp = Nothing
End Sub
Source: http://www.vbforums.com/showthread.php?536558-RESOLVED-Outlook-late-binding
this version also uses late binding, so you dont need any additional references.

How to add a Signature on Excel

I have an Excel spreadsheet Auditing Vendor documentation with expiry dates.
I have created an VBA macro which when I choose (Ctrl + M) will send an email requesting updates for specific documents based on the expiry dates.
Everything is beautiful and works like a charm.
My question is how do I include an Outlook Signature at the end of the email?
I would like it to pick up based on whoever has the spreadsheet open so that if Charlie Brown wants to trigger an email it would include Charlie Brown's Signature at the end.
It already auto-fills Charlie Brown as the Sender so I should be able to do this.
Any suggestions?
Here is an Example
Option Explicit
Sub AddSignature()
Dim olApp As Object
Dim olMail As Object
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
With olMail
.Display olMail.HTMLbody '<- adding default signature
End With
With olMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.HTMLbody = "Hello." & "<br>" & .HTMLbody '<- adding default signature
.Display
' .Send
End With
Set olMail = Nothing
Set olApp = Nothing
End Sub
also see Insert Signature in mail From Ron de Bruin
If you use excel to grab the new mail item signature you will get a flag for suspicious activity that the user could acknowledge
Dim OApp, OMail As Object
Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)
Dim sig As String
sig = OMail.HTMLbody
If you know the name of the signature you can go browse for it
Dir (CStr(Environ$("userprofile")) & "\appdata\roaming\microsoft\signatures\")

VBA Outlook Mail .display, recording when/if sent manually

My code displays a message with basic subject, body, attachment. Next the user manually updates and customizes the message and should send it. I want to record when (if) the email is sent. Is this possible or any tips?
My environment is Office 2007 with an excel based macro going to Outlook.
[Excerpt]
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = Email '.CC =
.Subject = Subj
.BodyFormat = olFormatHTML
.Body = Msg '.HTMLBody = Msg
If Not FileAttach = vbNullString Then .Attachments.Add (FileAttach)
.Display
End With
This is entirely possible, using the _Send event in the Outlook.MailItem class.
The way I use it, I create a class called EMail Watcher, so when I create the email and do the .Display, I then create a new EMailWatcher object and tell it to watch that email for send, then report back when it happens.
Here's the class as I use it. Basically, I also optionally can set the BoolRange so that if the user sends the email, that Excel range gets updated with True. I can also have the class update an Excel range with the time the email is sent.
Public BoolRange As Range
Public DateRange As Range
Public WithEvents TheMail As Outlook.MailItem
Private Sub TheMail_Send(Cancel As Boolean)
If Not BoolRange Is Nothing Then
BoolRange.Value = True
End If
If Not DateRange Is Nothing Then
DateRange.Value = Now()
End If
End Sub
And here's how I use it:
With oMail
.To = addr
.Subject = "CCAT eVSM Utilities License Code"
.Body = "Message body"
.Display
End With
Set CurrWatcher = New EmailWatcher
Set CurrWatcher.BoolRange = Range("G12")
Set CurrWatcher.TheMail = oMail
Hopefully that helps...