Need help sending an automated email in outlook using VBA while attaching a PDF from a folder location. Below is what I have thus far. I'm stuck on the part to attach the PDF
Sub ViolationProcessingTest()
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:11pt;font-family:Calibri>" & _
"Hello, <br><br> Please process the attached violations."
On Error Resume Next
With OutMail
.To = "email#email.com"
.CC = "email#email.com"
.Subject = "Violations Processing"
.Display
.HTMLBody = strbody & .HTMLBody
.Attachments.Add = "Folder path location with PDF"
End With
On Error GoTo 0
Set OutMail = Nothing
End Sub
Firstly, it is a good idea to get rid of the On Error Resume Next line - errors are raised for a reason, otherwise you won't even know why or where your code is failing.
Secondly, your code is almost OK, you just need to get rid of = (it is a method call, not a property assignment) and specify a fully qualified PDF file name:
.Attachments.Add "c:\SomeFolder\TheFile.PDF"
The Attachments.Add method (not a property) ceates a new attachment in the Attachments collection. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. So, the code should like that:
With OutMail
.To = "email#email.com"
.CC = "email#email.com"
.Subject = "Violations Processing"
.HTMLBody = strbody & .HTMLBody
.Attachments.Add filePathToYourFile
.Display
End With
You may find the How To: Create and send an Outlook message programmatically article helpful.
Related
I'm trying to attach a sheet on a mail, as a pdf file. But it's obviously not working (indeed, i'm there).
I know that the file is created (if I put a breakpoint or I remove the Kill fname the file is well created.
The mail is sent, with the body, the subject, from and to the good email address but the file is not attached. I don't know why, and it's making me crazy.
If anyone knows the soluce, please help me ! Thanks. Clément.
here's the macro. Creating the .pdf file, and then creating the email
fname = "testFile.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=fname, Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=False
Dim OutApp As Object
Dim OutMail As Object
Call Open_Outlook
On Error Resume Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "myMail#my.me"
.Body = "Body"
.SendUsingAccount = OutApp.Session.Accounts.Item(1)
.CC = ""
.BCC = ""
.Subject = "Subject"
.Attachments.Add fname
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
On Error GoTo 0
Kill fname
You need to specify the location of the PDF file, not just the name.
"C:\TEMP\testFile.pdf" or wherever you are saving it.
I am trying to embed a range from a worksheet as an image in outlook mail body. It's saving the picture correctly but I only see blank image in the outlook mail body. What am I doing wrong here?
Sub View_Email()
tName = Trim(MAIN.Range("tEmail"))
If Not tName Like "*#*.*" Then MsgBox "Invalid Email address": Exit Sub
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'File path/name of the gif file
Fname = ThisWorkbook.Path & "\Claims.jpg"
Set oCht = Charts.Add
STAT.Range("A3:G26").CopyPicture xlScreen, xlBitmap
With oCht
.Paste
.Export Filename:=Fname, Filtername:="JPG"
'.Delete
End With
On Error Resume Next
With OutMail
.To = tName
.CC = ""
.BCC = ""
.Subject = STAT.Range("C1").Value
.HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
"<img src=" & Fname & "' height=520 width=750>"
.display
'.Send 'or use .Display
End With
On Error GoTo 0
'Delete the gif file
'Kill Fname
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You need to add the image and hide it. The position 0 will add and hide it.
.Attachments.Add Fname, 1, 0
The 1 is the Outlook Constant olByValue
Once you add the image then you have to use "cid:FILENAME.jpg" as shown below.
Try this
With OutMail
.To = tName
.CC = ""
.BCC = ""
.Subject = STAT.Range("C1").Value
.Attachments.Add Fname, 1, 0
.HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
"<img src=""cid:Claims.jpg""height=520 width=750>"
.Display
End With
Screenshot
You need to set the PR_ATTACH_CONTENT_ID MAPI property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty and refer that attachment through the src attribute that matches the value of PR_ATTACH_CONTENT_ID set on the attachment. PR_ATTACH_CONTENT_ID corresponds to the Content-ID MIME header when the message is sent.
attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
MailItem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"
Keep in mind that setting content-id is preferable to using the attachment file name in the <img> element since it would need to be properly encoded and (if I remember correctly) some e-mail clients have a problem with just using the attachment file name for images.
I am doing an Automation on outlook where End user will receive a mail where their will be a link by clicking on which an auto reply mail will be trigger and go to respective person. The code i Tried So far is mentioned below.
Sub MailURL()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<HTML><BODY>"
strbody = strbody & "xxx#xxx.com"
strbody = strbody & "</BODY></HTML>"
On Error Resume Next
With OutMail
.to = "abc.domain.com"
.Subject = "Testing URL"
.HTMLBody = strbody
' .Send
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I understand that it is considered as a Spamming, but that was a requirement for our project, where we will send a mail with some data to end user and if user need some change in that data then he can click on that link and we receive a mail with requested changes so we make changes and update it in database. Thanks.
If I understand what you want, you need to add a link on the body that if clicked, will create a new mail with a pre-defined recipient. If that is so, this part:
"xxx#xxx.com"
should be:
"Relpy here"
You can also add a subject like this:
"Relpy here"
HTML syntax to add hyperlink is using your_linked_text. In your case, you need to add mailto: in front of the linked email address to create a new mail.
Note: This will not automatically send the reply (as you've said that can be considered spamming so let's not do that.)
here is a revision of your code
it worked for me (outlook2016), once i changed abc.domain.com to abc#domain.com
the "OutMail.Send" statement put the message into the outbox
the "OutMail.Display (True)" statement put the "new email" window on top of all other windows so that the user could verify the info and click "send"
Sub MailURL()
Dim strbody As String
strbody = "<HTML><BODY>"
strbody = strbody & "xxx#xxx.com"
strbody = strbody & "</BODY></HTML>"
Dim OutMail As Outlook.MailItem ' define the actual object ... then "help info" pops up on screen as you type
Set OutMail = Application.CreateItem(olMailItem) ' "Application" object already exists
With OutMail
.To = "abc#domain.com"
.Subject = "Testing URL"
.HTMLBody = strbody
' you can use only one of the following two lines
' .Send ' this puts the email message into the outbox
.Display (True) ' this show the email on top of everything else. just have the user click "send"
End With
Set OutMail = Nothing
End Sub
I have a VBA set of code that generates an email and automatically sends it.
One of the things that I cannot get to work properly is putting a hyperlink to a specified folder location within the email.
Dim fpath As String
fpath = Worksheets("MS_JRNL_OPEN_TU_FR-4333635").Range("AD5").Value
"file://" & fpath & _
Essentially the user has to input a folder location when running the Macro which is in Cell AD5, but I want this is appear as the full folder location as a hyperlink once the email is generated.
Any help would be greatly appreciated
If you are currently using HTMLBody in your email code, it's quite easy to do. I'll assume you are using code similar to below. Take note of strbody and .HTMLBody. Assuming your fpath is formatted like C:\Users\tjb1\Desktop\file.docx then you don't need to add anything else to it. The section creating the hyperlink is "test link". You can change test link to say whatever you want or change the line to "" & fpath & "" to use the path as the link text.
Sub MailURL()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "test link"
On Error Resume Next
With OutMail
.To = "APerson#Somewhere.com"
.Subject = "Testing URL"
.HTMLBody = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I found the code above at MrExcel and just formatted it a bit to work with your variable.
I am trying to send an email through vba in excel, all works fine excpect the email attachement. It doesnt seem to link it. Where could be the issue ?
The string attach is pointing to the right file.
Dim OutApp As Object
Dim OutMail As Object
Dim email
Dim attach
email = writeEmailAddress()
attach = attachement()
Sheets("Mail").Range("B1") = email
Sheets("Mail").Range("B2") = "xxxxxx"
Sheets("Mail").Range("B3") = "xxxxxxx"
Sheets("Mail").Range("B4") = attach
MsgBox attach
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.SendKeys "^{ENTER}"
.to = "xxxxx"
.CC = ""
.BCC = ""
.Subject = Sheets("Mail").Range("B5").Value
.Body = Sheets("Mail").Range("B6").Value
'You can add other files also like this
.Attachments.Add attach ' <--------------------------------This is causing troubble
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Change,
.Attachments.Add attach
... to,
If CBool(Len(Dir(attach, vbNormal))) Then
.Attachments.Add attach, 1 '<~~ 1 is olByValue
Else
Debug.Print "Cannot find '" & attach & "'"
End If
If the attachment is not added to your email item, check the VBE's Immediate Window (e.g. Ctrl+G) for the error message.