Mail is sent when the value in a cell of a specific row changes.
In addition we now want to send an Outlook task whenever that happens. The following first part is the email.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim OutApp As Object, OutMail As Object, strbody As String
If Target.Column = 44 Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Text "
On Error Resume Next
With OutMail
.To = Sheets("Param").Cells(3, 4)
.CC = ""
.BCC = ""
.Subject = "Text"
.Body = strbody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
End If
End Sub
Until here the code works. I've added the part about the task and although the code works without the IF THEN statement I can't get it to trigger with it or I get a 424 error.
Private Sub SendTask()
Dim objOut As Outlook.Application
Dim objTask As Outlook.TaskItem
Dim blnCrt As Boolean
If Target.Column = 6 Then 'modification numéro agrément
On Error GoTo CreateOutlook
Set objOut = GetObject(, "Outlook.Application")
CreateItem:
On Error GoTo 0
Set objTask = objOut.CreateItem(olTaskItem)
With objTask
.Assign
.Subject = "You need to fix this!"
.Body = "Please fix this problem by " & Format(Now + 10, "mm/dd/yy")
.DueDate = CDate(Now + 10)
.Recipients.Add ("youremail#domain.com")
.Display
End With
If blnCrt = True Then objOut.Quit
Set objTask = Nothing
Set objOut = Nothing
Exit Sub
CreateOutlook:
Set objOut = CreateObject("Outlook.Application")
blnCrt = True
Resume CreateItem
End If
End Sub
New version of the code that seems to work as intended
Private Sub Worksheet_Change(ByVal target As Range)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
If target.Column = 6 Then 'Modification of value in row 6
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olTaskItem)
With OutMail
.Assign
.Subject = "You need to fix this!"
.Body = "Please fix this problem by " & Format(Now + 10, "mm/dd/yy")
.DueDate = CDate(Now + 10)
.Recipients.Add ("youremail#domain.com")
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Den numèro d'agrément "
With OutMail
.To = Sheets("Param").Cells(3, 4)
.CC = ""
.BCC = ""
.Subject = "Fichier acquéreur: modification numéro agrément"
.Body = strbody
.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
End If
End Sub
Related
In Office 2013 this VBA code was used in Excel to send email via Outlook.
Function sendMail(reciever As String, cc As String, subject As String, body As String) As Boolean
Dim OutApp As Object
Dim OutMail As Object
Dim fileName As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'On Error GoTo err
With OutMail
.To = reciever
.cc = cc
.BCC = ""
.subject = subject
.BodyFormat = 2 'olFormatHTML
.HTMLBody = body
.Send
End With
sendMail = True
GoTo finally
err:
sendMail = False
finally:
Set OutMail = Nothing
Set OutApp = Nothing
End Function
In O365, on .Send command, I get
Run-time error '287'. Application-defined or object-defined error.
.Display is working.
Try the next way, please. Your adapted code should look like this:
Function sendMail(reciever As String, cc As String, subject As String, body As String) As Boolean 'functional (uneori eroare la .Send)
Dim OutApp As Object
Dim OutMail As Object
Dim fileName As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = reciever
.cc = cc
.BCC = ""
.subject = subject
.BodyFormat = 2 'olFormatHTML
.htmlBody = body
On Error Resume Next
.send
'the new part_________________________________________________
If err.Number <> 0 Then
err.Clear: On Error GoTo 0
.Save
If Not sendDrafts(reciever, subject) Then
MsgBox "Not possible to send it from ""Draft"", neither...": Exit Function
End If
End If
On Error GoTo 0
'_____________________________________________________________
End With
sendMail = True
GoTo finally
err:
sendMail = False
finally:
Set OutMail = Nothing
Set OutApp = Nothing
End Function
Copy the next function, too (preferable, in the same standard module):
Function sendDrafts(reciever As String, subject As String) As Boolean
Dim myOutlook As Outlook.Application, nSpace As Outlook.NameSpace
Dim mpFold As Outlook.Folders, draftFolder As Outlook.MAPIFolder, i As Long
Set myOutlook = Outlook.Application
Set nSpace = myOutlook.GetNamespace("MAPI")
Set mpFold = nSpace.Folders
'Set Draft Folder (use your Outlook account):
Set draftFolder = mpFold("xxxxx#xxxx.com").Folders("Draft")
For i = draftFolder.Items.count To 1 Step -1 'Loop through Draft folder Items
If Len(Trim(draftFolder.Items.item(i).To)) > 0 Then
If draftFolder.Items.item(i).To = reciever And _
draftFolder.Items.item(i).subject = subject Then
'Send Item
draftFolder.Items.item(i).send
sendDrafts = True
Exit For
End If
End If
Next i
End Function
If it does not work in this way, neither, you have a system (security) problem, which does not allow you to programmatically send mails...
I tested on my installation and both ways works.
I have a macro that works when I am at the office.
When working from Remote system we don't have Outlook and it will generate a error that it isn't possible to create an Outlook mail.
I need a MsgBox that says on the Remote there is no Outlook and then exit sub.
Sub Mail_workbook_Outlook_1()
Dim OutApp As Object
Dim OutMail As Object
Dim bodystr As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
bodystr = "Test"
ActiveWorkbook.Save
On Error Resume Next
With OutMail
.To = Worksheets("Test").Range("D25")
.CC = Worksheets("Test").Range("D26")
.BCC = ""
.Subject = Worksheets("Test").Range("D10")
.HTMLbody = bodystr
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I tried:
Set OutApp = CreateObject("Outlook.Application")
If OutApp Is Nothing Then
MsgBox "Outlook is not open, Open Outlook and try again!"
Exit Sub
Else
Set OutMail = OutApp.CreateItem(0)
End If
bodystr = "Test"
ActiveWorkbook.Save
On Error Resume Next
With OutMail
.To = Worksheets("Test").Range("D25")
.CC = Worksheets("Test").Range("D26")
.BCC = ""
.Subject = Worksheets("Test").Range("D10")
.HTMLbody = bodystr
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Try this code, please:
On Error Resume Next
Set OutApp = CreateObject("Outlook.Application")
Set Outmail = Outapp.Createitem(0)
If Err <> 0 Then
Err.Clear: On Error GoTo 0
MsgBox "No Outlook Application installed, or not configured": Exit Sub
End If
On Error GoTo 0
Tried all other codes on similar pages but failed to work.
This is my current version. Works only if I currently have a new email window open and oddly, my code will paste the .body and cell range details into 2 separate new email windows.
I just want the code to open a new email window with contents .body and cell range details (contains chart). Anybody have any ideas where my code went wrong?
Sub pasting01()
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.TO = "xyz#anc.com"
.CC = "abc#xyz.com"
.Subject = "Test"
.Body = "Dear Mr Lee" & vbNewLine
ActiveSheet.Range("A1:J30").Copy
Set vInspector = OutMail.GetInspector
Set wEditor = vInspector.WordEditor
wEditor.Application.Selection.Start = Len(.Body)
wEditor.Application.Selection.End = wEditor.Application.Selection.Start
wEditor.Application.Selection.Paste
.display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You have some errors on your code, try using Option Explicit top of your module
Option Explicit
Public Sub pasting01()
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.ActiveSheet
Dim rng As Range
Set rng = Sht.Range("A1:J30")
rng.Copy
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
Dim vInspector As Object
Set vInspector = OutMail.GetInspector
Dim wEditor As Object
Set wEditor = vInspector.WordEditor
With OutMail
.TO = "xyz#anc.com"
.CC = "abc#xyz.com"
.Subject = "Test"
.display
wEditor.Paragraphs(1).Range.Text = "Dear Mr Lee" & vbCr
wEditor.Paragraphs(2).Range.Paste
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Can you mess about with the following to suit your purpose?
Option Explicit
Sub pasting01()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim myChart As Chart
Set myChart = ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 1").Chart
Dim myPicture As String
Dim fileName As String
Dim myPath As String
myPicture = "Chart1.png"
myPath = "C:\Users\User\Desktop\"
fileName = myPath & myPicture
myChart.Export fileName
With OutMail
.TO = "xyz#anc.com"
.CC = "abc#xyz.com"
.Subject = "Test"
.Body = "Dear Mr Lee" & vbNewLine
.Attachments.Add fileName
.HTMLBody = "<html><p>First Line... </p>" & _
"<img src=cid:" & Replace(myPicture, " ", "%20") & " height=2*240 width=2*180>" & _
"<p>Salutation</p>" & _
"<p>" & "More text" & "</p></html>"
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Kill fileName
End Sub
Result:
I am trying to send a test mail from excel to Outlook, However I am getting error message : Run Time Error 287 in the following line :
OutMail.Send
Please find below my code:
Sub sendds()
Dim OutMail As MailItem
Dim outlookapp As Outlook.Application
Dim myInspector As Outlook.Inspector
Set outlookapp = CreateObject("Outlook.application")
Set OutMail = outlookapp.CreateItem(olMailItem)
With OutMail
.To = "email address"
.Subject = "test mail"
.Body = "Hi this is test email"
OutMail.Send 'Getting error on this line
End With
Set outlookapp = Nothing
Set OutMail = Nothing
End Sub
That is because you have incorrect email or email address format should be email#email.com or for testing purpose use .Display
Also change it to just .Send
With OutMail
.To = "email#address.com"
.Subject = "test"
.Body = "Hi this is test email"
.Send
End With
**For workaround **
With olMail
.To = "email"
.CC = ""
.BCC = ""
.Subject = ""
.Display
.Send
End With
try the below:
Public Sub emailUsFeature()
Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(olMailItem)
With outMail
.To = "abc#xyz.com; def#xyz.com"
.CC = "ghi#xyz.com"
.BCC = "jkl#xyz.com"
.Subject = "This is the subject."
End With
outMail.display
End Sub
Based on the comment "when I am using outMail.display it displays the email which I want to send but I actually want to send the email" the code is too fast. It would likely as well work if you stepped through with F8.
You could use Excel's Wait to delay the send.
This should as well work for all applications and it would be the minimum waiting period.
Sub sendds_ErrorHandlerWait()
Dim OutMail As MailItem
Dim outlookapp As Outlook.Application
Dim myInspector As Outlook.Inspector
Set outlookapp = CreateObject("Outlook.application")
Set OutMail = outlookapp.CreateItem(olMailItem)
With OutMail
.To = "email address"
.Subject = "test mail"
.body = "Hi this is test email"
On Error GoTo ErrorHandler
' Err.Raise 287 ' for testing
' Err.Raise 1 ' for testing
.Send
On Error GoTo 0
End With
ExitRoutine:
Set outlookapp = Nothing
Set OutMail = Nothing
Exit Sub
ErrorHandler:
Select Case Err
Case 287
DoEvents ' To accept clicks and to allow escaping if Outlook never opens
Debug.Print " <Ctrl> + <Break> to escape"
Resume
Case Else
On Error GoTo 0
' Break on other lines with an error
Resume
End Select
End Sub
It appears your Outlook setup requires a display. If there is no fix for that situation, you may be able to use an invisible display.
Sub sendds_InspectorRatherThanDisplay()
Dim OutMail As mailItem
Dim outlookapp As Outlook.Application
Dim myInspector As Outlook.Inspector
Set outlookapp = CreateObject("Outlook.application")
Set OutMail = outlookapp.CreateItem(olMailItem)
With OutMail
.To = "email address"
.Subject = "test mail"
.body = "Hi this is test email"
Set myInspector = .GetInspector
.Send
End With
ExitRoutine:
Set outlookapp = Nothing
Set OutMail = Nothing
Set myInspector = Nothing
End Sub
I am always adding in DoEvents and Application.Wait 1 to do this.
I usually don't display the email (and it is commented out here) so it just sends in the background. Works for me every time.
You obviously have to feed this sub from another with the arguments. An example of that is also here. (for example you could have the email address, file name etc. in each row and send an email dynamically for each row)
Sub LoopThroughTable()
For i = 2 To Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
email_to = Sheet1.Cells(i, 4).Value
email_subject = Sheet1.Cells(i, 3).Value
email_body = Sheet1.Cells(i, 8).Value
file_path = Sheet1.Cells(i, 2).Value & Sheet1.Cells(i, 3).Value
SendOutlookMessage email_to, email_subject, file_path, email_body
Next i
End Sub
Sub SendOutlookMessage(ByVal email_to As String, ByVal email_subject As String, ByVal file_path As String, ByVal email_body As String)
emailTo = email_to
emailSub = email_subject
FullPath = file_path
HTMLBODY = email_body
DoEvents
Application.Wait 1
Dim olApp As Object
Dim olMail As Object
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
With olMail
.to = emailTo
.Subject = emailSub
.Attachments.Add (FullPath)
.HTMLBODY = HTMLBODY
DoEvents
'.Display
Application.Wait 1
.Send
End With
Application.Wait 1
Set olMail = Nothing
Set olApp = Nothing
End Sub
Hope that helps.
I am working on a VBA script for mailing through Outlook in Excel 2010. Everything runs fine with one exception: the .SentOnBehalfofName line will not work. Here is the complete code
Sub Mail()
' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String ' This is for the Body of the email
Dim signature As String ' This is for the email signature
On Error Resume Next
'Set OutMail = Nothing
'Set OutApp = Nothing
Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail ' This inserts the email signature
.Display
End With
signature = OutMail.HTMLBody
With OutMail
'.Display
.To = sh.Range("C5")
.CC = sh.Range("C6")
.BCC = sh.Range("C7")
.Subject = sh.Range("C8").Value
.HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & signature
.SentOnBehalfOfName = sh.Range("C4").Value
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
If I remove this section the .SentOnBehalfOf works, but I lose my signature line:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail ' This inserts the email signature
.Display
End With
signature = OutMail.HTMLBody
If I put this back in the code, I get my signature line back, but I lose my ability to send on behalf of another party.
I'm looking for a solution that allows me to do both. Any help would be appreciated.
Here is my solution. I needed to move the .SentOnBehalfOfName to the first statement in the WITH Command, then .Display immediately after that. I replace the string for signature line with .HTMLBody to pull in the signature line. Code runs fine now!
I don't know why the statements need to be in this order, but it works.......
Sub Mail()
' Working in Office 2010-2013
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String ' This is for the Body of the email
On Error Resume Next
'Set OutMail = Nothing
'Set OutApp = Nothing
Dim sh As Worksheet
Set sh = Sheets("Mail")
strbody = sh.Range("C9").Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SentOnBehalfOfName = sh.Range("C4")
.Display
.To = sh.Range("C5")
.CC = sh.Range("C6")
.BCC = sh.Range("C7")
.Subject = sh.Range("C8").Value
.HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub