If error then skip sending an email - vba

I have a piece of code that sends me an email when a button is pressed.
However if for some reason the email won't send I don't want the code to break, I would like it to just ignore this command and carry on. How do I add that into my current code?
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = ThisWorkbook.Name & vbNewLine & _
Environ("username")
On Error Resume Next
With OutMail
.To = "bm#Email.co.uk"
.CC = ""
.BCC = ""
.Subject = "OGI Statements Used"
.Body = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Thank you

You could change your error statement to avoid the .Send method in the case of an earlier error - but if the error occurs with the actual sending of the email then there's not a lot you can do as the execution has been handed over to another application and so it's out of the error handler's scope so to speak.
See if this is of any use:
On Error GoTo Skip:
With OutMail
.To = "bm#Email.co.uk"
.CC = ""
.BCC = ""
.Subject = "OGI Statements Used"
.Body = strbody
.Send
End With
Skip:
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

Related

Sending email from other account

I send emails through Outlook using Excel VBA. I am using my work computer with my work email as the main account, but want to send from another account that is logged in.
I have not managed to integrate any of the code found online.
The below code is without my attempt of fixing it.
Sub Test1()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strbody As String
For Each cell In Range("D2:D2")
strbody = strbody & cell.Value & vbNewLine
Next
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("A").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "A personal message from the founder"
.Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Do you mean send from another email address? If so just add :
.SentOnBehalfOfName = "Email#Other.com" 'Change to the email address you want to send from
e.g
With OutMail
.To = cell.Value
.SentOnBehalfOfName = "Email#Other.com"
.Subject = "A personal message from the founder"
.Body = "Hi " & Cells(cell.Row, "B").Value & vbNewLine & vbNewLine & strbody
.Send
End With
There are two possible ways in Outlook:
If another account is configured in Outlook you need to use the MailItem.SendUsingAccount property which returns or sets an Account object that represents the account under which the MailItem is to be sent.
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
If you have got permissions set by the Exchange admin to send on behalf of another person you need to use the MailItem.SentOnBehalfOfName property which returns a string indicating the display name for the intended sender of the mail message.

How to attach PDF files from a folder?

I need to attach variable PDFs from a transport folder.
Sub send_attachent()
Dim OutApp As Object
Dim OutMAil As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMAil = OutApp.CreateItem(0)
strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
"Please find attached High Risk Defect:<br><br> AT300-HRD-00<br><br> Issue<br><br>" & _
"Regards,<br>"
On Error Resume Next
With OutMAil
.TO = "julia.naydenova#hitachirail.com"
.CC = "jean.ash#hitachirail.com"
.BCC = ""
.Subject = "AT300-HRD-00"
.Display
.HTMLBody = strbody & .HTMLBody
.Attachments.Add "I:\ServiceDelivery\MaintenanceManuals\AT300\TRANSPORT FOLDER\AT300-HRD-00031 Test.pdf"
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
End With
MsgBox "Email Sent"
On Error GoTo 0
Set OutMAil = Nothing
End Sub
I need to send variable files, so whatever I put in the folder to be attached on the email. With the file name in the macro I can only send one file.
The Outlook object model doesn't provide anything for monitoring files in a folder on the disk. You need to create a file-watcher which can monitor files in a folder and create a new mail item when a file is added to the folder. See VBA monitor folder for new files for more information on that.
Loop through files in a folder with Dir.
Option Explicit
Sub send_all_PDF_folder()
Dim outApp As Object
Dim outMail As Object
Dim strbody As String
Dim filePath As String
Dim fileName As String
Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(0)
strbody = "<BODY style = font-size:12pt; font-familt:Arial>" & _
"Please find attached High Risk Defect:<br><br> AT300-HRD-00<br><br> Issue<br><br>" & _
"Regards,<br>"
filePath = "I:\ServiceDelivery\MaintenanceManuals\AT300\TRANSPORT FOLDER"
With outMail
.To = "someone#somewhere.com"
.CC = "someoneCC#somewhere.com"
.Subject = "AT300-HRD-00"
.Display
.HtmlBody = strbody & .HtmlBody
fileName = dir(filePath & "\*.pdf")
Do While fileName <> ""
.Attachments.Add filePath & fileName
fileName = dir
Loop
End With
Set outMail = Nothing
Set outApp = Nothing
End Sub

failes to send mail after first in loop

Got a macro running through columns with mail addresses using most parts from Ron de Bruin.
The macro runs through just fine, but only sends the first hit in column B and doesn't display any of the other hits when i try to watch it? What could be the issue?
The code is so that I can get the default signature from outlook, thats why it's .Display first in the code.
Sub mail_HTML()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
strbody = "<H3>Hei " & Cells(cell.Row, "E").Value & "</H3>" _
& "<p>" & Range("k4") & "<p>"
On Error Resume Next
With OutMail
.Display
.To = cell.Value
.Subject = Range("K12").Value
.HTMLBody = strbody & .HTMLBody
'You can add files also like this
'.Attachments.Add Range("O1").Value
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
When you set
Set OutMail = Nothing
You dont have any access to the object anymore (because it is destroyed). But you set it before the loop. You need to set it in every loop then like this:
On Error Resume Next
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
.To = cell.Value
.Subject = Range("K12").Value
.HTMLBody = strbody & .HTMLBody
'You can add files also like this
'.Attachments.Add Range("O1").Value
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
So after 1 Email the oject is destroyed, but you are not aware because of the on error resume next
Tried that but it is not working for me, here is my code:
Do Until in_file.EOF
Email_To = in_file!email_address
Email_Bcc = ""
Email_Body = in_file!email_salut & " " & in_file!email_name & ", test this."
Email_Subject = "Email Subject"
Set mail_object = CreateObject("Outlook.Application")
Set mail_single = mail_object.CreateItem(0)
With mail_single
.Subject = Email_Subject
.To = Email_To
.cc = Email_Cc
.BCC = Email_Bcc
.Body = Email_Body
.send
End With
Set mail_object = Nothing
Set mail_single = Nothing
in_file.MoveNext
Loop

Attach two files in a mail

I would like to create a VBA script which attaches two files to a mail. This code lets me attach one file:
Sub openWord()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
var2 = "D:\Werkdocumenten\CBIP\CBIP_MANUAL_PART1.pdf"
var3 = "D:\Werkdocumenten\CBIP\CBIP_MANUAL_PART2.pdf"
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "CBIP Manual"
.Body = ""
.Attachments.Add (var2)
.Display
End With
End Sub
Only if I change .Attachments.Add (var2) to .Attachments.Add (var2 & var3) it does not work. Anybody clues on how I can attach the second attachment?
.Attachments.Add var2
.Attachments.Add var3

Send Email from different email accounts

When I send emails they go out from my personal inbox at work, but I need them to go out from another mailbox that I have access to. I have both mailboxes in my outlook.
Can I send emails from a specific mailbox?
Here is the code I am using. It does not find the 2nd item in my account.
Sub Mail_small_Text_Change_Account()
Dim cel As Range
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
For Each cel In Range(("C2"), Range("C2").End(xlDown))
With OutMail
.To = cel.Value
'.CC = cel.Offset(0, 3).Value
.Subject = "Benefits Refund"
.Body = strbody
.SendUsingAccount = OutApp.Session.Accounts.Item(2)
.Send 'or use .Display
End With
Next
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Yes. Using a POP mail box, you can specify the user name and password for any account.
.To = "E#E.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "This is the body"
.SendUsingAccount = OutApp.Session.Accounts.Item(1)