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
Related
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
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
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
How do I include rows from an Access query/form in a VBA generated emails?
I currently send the forms as attachments, but not every client has Excel, so that's why I want to include it in the e-mail body, which is HTML, I think.
This is my code:
Option Compare Database
Private Sub btnMail_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim MailTo, Subj, Att, Att2, Att3 As String
MailTo = "myemail#gmail.com"
Subj = "Test mail"
Content = "Private content"
Att = "C:\Users\min stock.xls"
Att2 = "C:\Users\todo.txt"
Att3 = "C:\Users\Stockmanagementv3.accdb"
staff = "Kind regards" & vbCrLf & "My company"
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = MailTo
.Subject = Subj & Now & " Mail "
.Body = Content & staff
.Attachments.Add Att
.Attachments.Add Att2
.Attachments.Add Att3
.send
End With
Set OutMail = Nothing
Set OutApp = Nothing
MsgBox ("Your mail has been sent succesfully!")
End Sub
as mentioned in my comment you could save the query as html file
dim FN as String
fn = "C:\myQuery.html"
'SAve the query as html file
docmd.OutputTo acOutputQuery, "qry_name", acFormatHTML, fn
'add the file as attachment
.Attachments.Add fn
hope this helps..
I'm writing a VBA query which automatically sends an email when you press a button in word. I would also like it to attach a certain file. Problem is however the file it needs to attach has a name which has an element in it which changes (like report+weeknr, the date part changes)
But because I also include the weeknr into the subject of the mail (like "Subject: report+weeknr") i though I could automate the attaching of the document through create a variable which is a result from report + weeknr. It does not work however. Anybody an idea how i can get this working? See code below:
Sub Sendmessage()
Dim OutApp As Object
Dim OutMail As Object
Dim var1 As String
Dim sentto As Long
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
var1 = InputBox("Insert week")
'Line below is where it goes wrong. Var2 leads to C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt
var2 = "C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt"
With OutMail
.To = "marcvanderpeet#gmail.com; marc#gmail.com"
.CC = ""
.BCC = ""
.Subject = "Report_" & var1
.Body = "Text"
.Attachments.Add (var2)
.Display
End With
End Sub
Sub Sendmessage()
Dim OutApp As Object
Dim OutMail As Object
Dim var1 As String
Dim sentto As Long
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
var1 = InputBox("Insert week")
'Line below is where it goes wrong. Var2 leads to C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt
var2 = "C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt"
With OutMail
.To = "marcvanderpeet#gmail.com; marc#gmail.com"
.CC = ""
.BCC = ""
.Subject = "Report_" & var1
.Body = "Text"
.Attachments.Add (var2)
.Display
End With
End Sub