Attachment on mail - vba

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.

Related

Outlook VBA to send Automated email with PDF attachment

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.

Embed picture in outlook mail body excel vba

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.

Insert filepath as hyperlink Excel VBA

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.

attachement in email VBA excel

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.

Add signature with images to the Mail

I have a macro for Outlook where I can create a complete mail with an attachment but can not add a signature saved in my C drive (C:\Users\JustinG\AppData\Roaming\Microsoft\Signatures).
Signature types are .rtf and .htm with images.
The following is the code:
Sub Mail_Workbook_1()
Dim OutApp As Object
Dim Outmail As Object
Set OutApp = CreateObject("Outlook.Application")
Set Outmail = OutApp.CreateItem(0)
On Error Resume Next
' Change the mail address and subject in the macro before you run it.
With Outmail
.SentOnBehalfOfName = "justin.gatlin#rediffmail.com"
.To = "abc#xyz.com"
.CC = ""
.BCC = ""
.Subject = "Presentation"
.Body = "Hi Team,"
.Attachments.add ("C:\Users\DurshetwarA\Desktop\Excel Examination_Master_V1.xlsx")
.display
''SendKeys ("%s")
End With
On Error GoTo 0
Set Outmail = Nothing
Set OutApp = Nothing
End Sub
In the .htm file in the signatures directory you can edit the htm file. The pictures are stored as relative path and when you use the code it looses that path so if you use discrete path it will be able to find the pictures. so go into the file and look for any relative paths and make them discrete.
"/Microsoft/Signatures/picturefile.jpg"
change that to include the whole path
"/root/user/blah blah../Microsoft/Signatures/picturefile.jpg"
This solved the missing image problem for me.
Solution described here by Ron de Bruin.
Sub Mail_Outlook_With_Signature_Html_2()
' Don't forget to copy the function GetBoiler in the module.
' Working in Office 2000-2013
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim SigString As String
Dim Signature As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "<H3><B>Dear Customer Ron de Bruin</B></H3>" & _
"Please visit this website to download the new version.<br>" & _
"Let me know if you have problems.<br>" & _
"Ron's Excel Page" & _
"<br><br><B>Thank you</B>"
'Change only Mysig.htm to the name of your signature
SigString = Environ("appdata") & _
"\Microsoft\Signatures\Mysig.htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = strbody & "<br>" & Signature
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Instead of .body use .htmlbody and design your message body in HTML. This is the only way of inserting image in your message. There is no specific option to insert signature
Similar to the solution posted by Adavid02, here you may find a more detailed explanation.