I have the following code written (based on other posts here in SO) to insert a hyperlink to the folder that the workbook is saved in on a network drive into the body of an Email and the link does not show up in the body of the Email and I am at a loss as to why. Any help is greatly appreciated.
I do have the Microsoft Outlook Object Library checked in the References.
I have tried hyperlink = "" and hyperlink = "" to no avail.
Below is the full code:
Private Sub FileToApprRev_Click()
Dim OutlookApp As Object, MItem As Object
Dim Subj As String, EmailAddr As String, Recipient As String
Dim LienPos As Range, clsDate As Range, address As String, lNum As Range, Street As Range, City As Range, State As Range, ZipCode As Range, CustName As Range
Dim strBody As String, Email As String, hyperlink As String, currDir As String
Set wb = Application.ThisWorkbook
Set wsSI = wb.Sheets("SavedInfo")
Set Street = wsSI.Range("Street")
Set City = wsSI.Range("City")
Set State = wsSI.Range("State")
Set ZipCode = wsSI.Range("Zip")
Set lNum = wsSI.Range("Loan_Number")
Set clsDate = wsSI.Range("Closing_Date")
Set LienPos = wsSI.Range("Lien_Position")
Set CustName = wsSI.Range("PBName")
address = Street & ", " & City & ", " & State & " " & ZipCode
Set OutlookApp = CreateObject("Outlook.Application")
Set MItem = OutlookApp.CreateItem(0)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Email = "SpongeBob#UnderTheSeaHeaven.com"
currDir = wb.path
hyperlink = ""
Debug.Print hyperlink
strBody = "<p>" & "Hello , " & "<br><br>" & vbNewLine & vbNewLine & _
"Please complete the Appraisal Review for the file below." & "</p>" & vbNewLine & _
hyperlink
With MItem
.Display
.to = Email
.Subject = "ATTN - Appraisal Review" & " - " & CustName & " - " & clsDate
.HTMLBody = strBody & "<br>" & .HTMLBody
.send
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I figured it out. I was using the incorrect structure for the file directory.
target = "\\fsps02\users\......"
Related
I am using a macro to compose a report based on an excel file. The macro uses a body with text and a picture (png) from an predefined Excel range.
This used to work perfect but now I have to share the report outside of my organization i get feedback that the image is not showing.
Does anybody know if this is due to the macro or not?
I have tested this also to my hotmail and gmail accounts and it is not showing there as well?
Sub Mail_()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim SigString As String
Dim Signature As String
Dim Adm As Worksheet
Dim Body As String
Dim Body2 As String
Dim Body3 As String
Dim Body4 As String
Dim rngToPicture As Range
Dim rng2 As Range
Dim Weeknr As String
Dim strTempFilePath As String
Dim strTempFileName As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set Adm = ActiveWorkbook.Worksheets("Uit")
Set rngToPicture = Adm.Range("X13:AT65")
Adm.Activate
ActiveWindow.Zoom = 100
strTempFileName = "RangeAsPNG"
Weeknr = Adm.Range("AF3").Text
Body = Adm.Range("X6:X6").Text
Body2 = Adm.Range("X8:X8").Text
Body3 = Adm.Range("X9:X9").Text
Body4 = Adm.Range("X11:X11").Text
strbody = "<BODY style=font-size:10pt;font-family:Verdana>" & Body & _
"<br><br>" & Body2 & "<br>" & Body3 & "<br><br>" & Body4 & "<br><br>"
'Change only Mysig.htm to the name of your signature
SigString = Environ("appdata") & _
"\Microsoft\Handtekeningen\Servicekantoor.htm"
Signature = GetBoiler(SigString)
On Error Resume Next
With OutMail
.to = "Mailinglist#list.com"
.CC = ""
.BCC = ""
.Subject = "Weekly report " & Weeknr
'Create the range as a PNG file and store it in temp folder
Call createPNG(rngToPicture, strTempFileName)
'Embed the image in Outlook
strTempFilePath = Environ$("temp") & "\" & strTempFileName & ".png"
.Attachments.Add strTempFilePath, olByValue, 0
.HTMLBody = strbody & "<br><br>" & "<img src='cid:" & strTempFileName & ".png'
style='border:0'>" & "<br><br>" & Signature
.Recipients.ResolveAll
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You are close - the cid in the src attribute must be not the file name (which is not visible to the outside users), but some value that matches the PR_ATTACH_CONTENT_ID property on the attachment:
strTempFilePath = Environ$("temp") & "\" & strTempFileName & ".png"
set Attach = .Attachments.Add(strTempFilePath, olByValue)
Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid"
.HTMLBody = strbody & "<br><br>" & "<img src='cid:MyCid' style='border:0'>" & "<br><br>" & Signature
I'm trying to attach three sheets to an email to be sent to a certain email address with a certain subject and content.
I currently attach each sheet in the workbook to an email each.
The two problems I'm looking to solve -
It currently cycles through all sheets, I want to attach sheets labeled "Account", "Subscription", "Users" so I can have another sheet for instructions.
Can I get attach all three to a single email? My research so far has come up blank.
I tried using something like the below, but that created errors in other areas that I don't know.
For Each ws In Sheets(Array("Account", "Subscription", "Users"))
Sub COMEON()
Dim onePublishObject As PublishObject
Dim oneSheet As Worksheet
Dim scriptingObject As Object
Dim outlookApplication As Object
Dim outlookMail As Object
Dim htmlBody As String
Dim htmlFile As String
Dim textStream, fil As String
Dim dummy As Workbook
Dim var As String
var = Range("A1").Value
Today = Format(Now(), "dd-mm-yyyy")
Set dummy = ActiveWorkbook
Set scriptingObject = CreateObject("Scripting.FileSystemObject")
Set outlookApplication = CreateObject("Outlook.Application")
For Each oneSheet In ActiveWorkbook.Worksheets
Dim StrBody As String
StrBody = " THIS IS A TEST" & " " & UCase(oneSheet.Name) & " " & "XYZ," & vbNewLine & _
vbNewLine & _
"Please FIND ATTACHED <B>'XYZ REPORT'<B>"
Application.DisplayAlerts = False
Sheets(oneSheet.Name).Copy
ActiveWorkbook.SaveAs dummy.Path & "\" & oneSheet.Name & ".csv"
ActiveWorkbook.Close
Application.DisplayAlerts = True
Set outlookMail = outlookApplication.CreateItem(0)
With outlookMail
.To = "XXXXX#XXXXX.com"
.htmlBody = StrBody & htmlBody
.attachments.Add dummy.Path & "\" & oneSheet.Name & ".csv"
.Display
.Subject = var & " - " & UCase(oneSheet.Name) & " CSV " & "(" & Today & ")"
End With
Next oneSheet
End Sub
Should be close:
Sub COMEON()
Dim oneSheet As Worksheet
Dim scriptingObject As Object
Dim outlookApplication As Object
Dim outlookMail As Object
Dim htmlBody As String
Dim dummy As Workbook
Dim var As String
Dim StrBody As String, arrSheets, Today
var = Range("A1").Value
Today = Format(Now(), "dd-mm-yyyy")
Set dummy = ActiveWorkbook
Set outlookApplication = CreateObject("Outlook.Application")
Set outlookMail = outlookApplication.CreateItem(0)
With outlookMail
.To = "XXXXX#XXXXX.com"
.bodyformat = 1 'HTML
.Subject = var & " - CSV " & "(" & Today & ")"
.Display
End With
StrBody = "THIS IS A TEST<br><br>Files: <ul>"
arrSheets = Array("Account", "Subscription", "Users")
For Each oneSheet In dummy.Worksheets
If Not IsError(Application.Match(oneSheet.Name, arrSheets, 0)) Then
StrBody = StrBody & "<li>" & oneSheet.Name & "</li>"
Application.DisplayAlerts = False
Sheets(oneSheet.Name).Copy
ActiveWorkbook.SaveAs dummy.Path & "\" & oneSheet.Name & ".csv"
ActiveWorkbook.Close
Application.DisplayAlerts = True
'add attachment
outlookMail.attachments.Add dummy.Path & "\" & oneSheet.Name & ".csv"
End If 'want this sheet
Next oneSheet
With outlookMail
.htmlBody = StrBody & "</ul>" & .htmlBody
End With
End Sub
Basically move stuff out of the loop that doesn't need to be there.
I'm attempting to add attach a file to an email created from a template. The idea is to be able to use the File Picker to select multiple files and excel sends an email to the proper recipients with the correct attachments.
The problem is that I cannot use the ".Display" method without getting an error and I want to review the email before sending so I do not want to use ".Send".
However, for whatever reason, if I clear the email template body with ".Body = ''", I am able to Display the email and attach the correct file. I'd like to keep the email body from the template as is though without clearing it and rewriting it.
So it seems that I cannot use an email template if I want to first display before sending? Has anyone ever had this problem or know how to solve?
The Error message is:
'-2147221233(8004010f)' The attempted operation failed. An object could not be found.
Btw, most of the variables are declared globally so that is why they are not visible.
Dim Agency As String
Dim xfullName As Variant
Dim Template As String
Dim mail As Outlook.mailItem
Dim myOlApp As Outlook.Application
Dim selectedFile As Variant
Dim emailBody As String
Dim emailType As String
Dim recipients As String
Sub Recall_Email()
Dim fileName As String
Dim inputFile As FileDialog
Set myOlApp = CreateObject("Outlook.Application")
Set inputFile = Application.FileDialog(msoFileDialogFilePicker)
Template = "C:\Users\me\AppData\Roaming\Microsoft\Templates\Recall Templates\Recall Template.oft"
With inputFile
.AllowMultiSelect = True
If .Show = False Then Exit Sub
End With
For Each selectedFile In inputFile.SelectedItems
xfullName = selectedFile
fileName = Mid(inputFile.SelectedItems(1), InStrRev(inputFile.SelectedItems(1), "\") + 1, Len(inputFile.SelectedItems(1)))
Agency = Left(fileName, 3)
CreateTemplate(Template)
Next selectedFile
End Sub
Private Sub CreateTemplate(temp)
Set myOlApp = CreateObject("Outlook.Application")
Set mail = myOlApp.CreateItemFromTemplate(temp)
Set olAtt = mail.Attachments
With mail
'.Body = "" -- If I use this line, everything attaches
.Subject = Agency & " Recall File"
.To = "email"
.Attachments.Add xfullName
.Display '.Send
End With
End Sub
Here is a working example on how to attach or embed files to outlook.
Option Explicit
Dim titleName As String
Dim firstName As String
Dim lastName As String
Dim fullName As String
Dim clientEmail As String
Dim ccEmail As String
Dim bccEmail As String
Dim emailMessage As String
Sub GenerateInfo()
Dim WS As Worksheet
Dim lrow As Long
Dim cRow As Long
Set WS = ActiveSheet
With WS
lrow = .Range("E" & .Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For cRow = 2 To lrow
If Not .Range("L" & cRow).value = "" Then
titleName = .Range("D" & cRow).value
firstName = .Range("E" & cRow).value
lastName = .Range("F" & cRow).value
fullName = firstName & " " & lastName
clientEmail = .Range("L" & cRow).value
Call SendEmail
.Range("Y" & cRow).value = "Yes"
.Range("Y" & cRow).Font.Color = vbGreen
Else
.Range("Y" & cRow).value = "No"
.Range("Y" & cRow).Font.Color = vbRed
End If
Next cRow
End With
Application.ScreenUpdating = True
MsgBox "Process completed!", vbInformation
End Sub
Sub SendEmail()
Dim outlookApp As Object
Dim outlookMail As Object
Dim sigString As String
Dim Signature As String
Dim insertPhoto As String
Dim photoSize As String
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
'Change only Mysig.htm to the name of your signature
sigString = Environ("appdata") & _
"\Microsoft\Signatures\Marius.htm"
If Dir(sigString) <> "" Then
Signature = GetBoiler(sigString)
Else
Signature = ""
End If
insertPhoto = "C:\Users\marius\Desktop\Presale.jpg" 'Picture path
photoSize = "<img src=""cid:Presale.jpg""height=400 width=400>" 'Change image name here
emailMessage = "<BODY style=font-size:11pt;font-family:Calibri>Dear " & titleName & " " & fullName & "," & _
"<p>I hope my email will find you very well." & _
"<p>Our <strong>sales preview</strong> starts on Thursday the 22nd until Sunday the 25th of November." & _
"<p>I look forward to welcoming you into the store to shop on preview.<p>" & _
"<p> It really is the perfect opportunity to get some fabulous pieces for the fast approaching festive season." & _
"<p>Please feel free to contact me and book an appointment." & _
"<p>I look forward to seeing you then." & _
"<p>" & photoSize & _
"<p>Kind Regards," & _
"<br>" & _
"<br><strong>Marius</strong>" & _
"<br>Assistant Store Manager" & _
"<p>"
With outlookMail
.To = clientEmail
.CC = ""
.BCC = ""
.Subject = "PRIVATE SALE"
.BodyFormat = 2
.Attachments.Add insertPhoto, 1, 0
.HTMLBody = emailMessage & Signature 'Including photo insert and signature
'.HTMLBody = emailMessage & Signature 'Only signature
.Importance = 2
.ReadReceiptRequested = True
.Display
.Send
End With
Set outlookApp = Nothing
Set outlookMail = Nothing
End Sub
Function GetBoiler(ByVal sFile As String) As String
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
I have an e-mail that is generated through Excel with VBA. This e-mail includes two embedded pictures in the body of the e-mail along with the separate hyperlinks to the videos they refer to. The problem is that it isn't recognizing the second picture and just embedding the same picture twice, however the hyperlinks are correct. Below is a sample of my code:
Private Sub SubmitBtn_Click()
Dim Source As Range
Dim Dest As Workbook
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim OutApp As Object
Dim OutMail As Object
Dim MemNme As String, Email As String, UsrName As String, domainID As String, pic As String, pic2 As String
Dim Hlink As String, Hlink2 As String
State = Screener.StateBox
If State = "California" Then
If Screener.MktPlcBox = True Then
pic = "websitewithpicture1"
Hlink = "videolink"
count = 1
End If
If Screener.SubsidyBox = True Then
pic2 = "websitewithpicture2"
Hlink2 = "videolink"
count = 2
End If
With OutMail
.To = Email
.CC = ""
.BCC = ""
.Subject = "Helpful Video"
.HTMLBody = "Dear " & MemNme & ",<br><br>" _
& vbNewLine & vbNewLine & "Thank you for speaking with me today about your plan. You have a lot of choices, " _
& " and <b>we appreciate you choosing company</b>. Helping you understand your plan is important to us and I thought this video would be valuable to you.<br><br>" _
& vbNewLine & "<center><a href=" & Hlink & "<img src=cid:" & Replace(pic, " ", " ", "520") & " height =250 width=400></a>" _
& "<a href=" & Hlink2 & "<img src=cid:" & Replace(pic2, " ", " ", "420") & " height =250 width=400></a></center><br>" _
& vbNewLine & vbNewLine & "You can always get additional information at <b>website.com</b> or by calling the number on the back of your card.<br><br>" _
& vbNewLine & vbNewLine & "Thank you,<br>" _
& vbNewLine & UsrName
.Attachments.Add pic, olByValue, 0
.Attachments.Add pic2, olByValue, 0 <--------It doesn't "See" this pic???
' MsgBox "Press ok to create your e-mail"
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Unload Me
End Sub
Ok So I'm stuck on this Excel VBA code. I have it automatically generating emials in outlook with information from the sheet already inputed. BUT When i try to move a Cell thats formatted as [h]:mm:ss it puts a decimal version of the number in the outlook email.
Sub Mail_test()
'Set variables/objects for code.
Dim OutApp As Object
Dim OutMail As Object
Dim eto As String
Dim ecc As String
Dim esub As String
Dim ebody As String
Dim ebody1 As String
Dim ebody2 As String
Dim ebody3 As String
Dim ebody4 As String
Dim intbody As String
Dim wkDay As String
Dim otOff As String
Dim intbody2 As String
Dim intbody3 As String
Dim ebody6 As String
Dim intbody4 As String
Dim intbody5 As String
Dim ebody7 As String
Dim Ebody10 As String
Dim ebody11 As String
Dim Ebody12 As String
Dim intbody12 As String
Dim intbody13 As String
Dim Ebody13 As String
Dim ebody15 As String
Dim ebody20 As String
Dim Ebody21 As String
Dim Ebody22 As String
Dim Tempefile As String
'Sets application to update with code executions.
With Application
.ScreenUpdating = True
.EnableEvents = False
End With
'Conditional to determine if the code should continue.
mydate = E3
'Sets default body of the email.
Ebody12 = "Hello "
intbody12 = Sheets("Emails").Range("b3")
ebody15 = "," & vbNewLine & vbNewLine
Ebody13 = "We are doing our Bi-Weekly Aux 2 Audit. " & vbNewLine & vbNewLine
ebody11 = "This Escalation is for Agent: "
Ebody10 = Sheets("Emails").Range("AgentName").Value & vbNewLine & vbNewLine
ebody1 = "Your agent was over the allotted time for aux 2 for the two week period :"
ebody2 = "Agents are allotted 1.33% of their staffed time. " & vbNewLine & vbNewLine
ebody3 = "Your agents staffed time was : "
intbody = Sheets("Emails").Range("stafftime") & vbNewLine & vbNewLine
ebody4 = "Your Agents Aux 2 percentage for the last two weeks was : "
intbody2 = Sheets("Emails").Range("auxper").Value
ebody5 = "????"
intbody3 = Sheets("Emails").Range("F3").Value
ebody6 = " Can we please have this coached? " & vbNewLine & vbNewLine
intbody4 = Sheets("Emails").Range("g3").Value & vbNewLine & vbNewLine
ebody7 = "Your agents Aux 2 time was :"
intbody5 = Sheets("Emails").Range("Auxhours").Value & vbNewLine & vbNewLine
intbody13 = Sheets("Emails").Range("I3").Value & vbNewLine & vbNewLine
ebody20 = "Thank you,"
Ebody21 = " - "
Ebody22 = "%" & vbNewLine & vbNewLine
' Begins loop for adherence entry, loops until no further records exist to enter.
'Resets variables for body of the email.
intbody = ""
ebody = ""
'Conditional, if agent doesn't have overtime for tomorrow, skips to next agent..
'Continues to reset variables for email communication
eto = Sheets("Emails").Range("Supname") & ";" & Sheets("Emails").Range("managerName") 'sets the value in email data tab for To field
ecc = "christopher.meyers#pace.com" 'sets the value in email data tab for CC field
esub = "Aux 2 Escalation" 'sets the value in email data tab for Subject field
intbody = Sheets("Emails").Range("d3").Value & vbNewLine & vbNewLine
ebody = Ebody12 + intbody12 + ebody15 + ebody11 + Ebody10 + Ebody13 + ebody + ebody1 + intbody3 + inbody + Ebody21 + intbody4 + ebody2 + ebody3 + intbody + ebody7 + intbody5 + ebody4 + intbody2 + Ebody22 + ebody6 + ebody20
'Starts outlook application to send email.
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Sets variables for email.
On Error Resume Next
With OutMail
.To = eto
.CC = ecc
.BCC = ebcc
.Subject = esub
.Body = ebody
.Importance = 2
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
End With
' Unlocks outlook
Set OutMail = Nothing
Set OutApp = Nothing
' Removes the row and loops back to the beginning to enter a new record.
Application.Wait Now + TimeValue("00:00:02")
Rows(6).EntireRow.Delete
End Sub
These two are the ones that I am wanting to show up [H]:MM:SS format
intbody5 = Sheets("Emails").Range("Auxhours").Value & vbNewLine & vbNewLine
intbody = Sheets("Emails").Range("stafftime") & vbNewLine & vbNewLine
I've tried
tempfile = Sheets("Emails").Range("stafftime") & Format (Time, "[H]:MM:SS")
and using the tempfile instead of the intbody in the body portion of the coding but it just retunrs the same numbers with 00:00:00 at the end.
Any Help wwould be appreicated
Use .text instead of .value
.text puts it as you view it in the cell
intbody5 = Sheets("Emails").Range("Auxhours").text & vbNewLine & vbNewLine