Quotation Marks - VBA - html

I am trying to link a PNG file as image source for a signature - to send e-mail using vba. I am trying to adjust the size of the signature (PNG Image) as well trying to understand what a Single Quote and Double quote refers to. I need image size to be Height - 1.96 cm and width - 16.37 cm. Please advise.
"<img src ='" & relativepath1 & "'>" **<--- This one works but signature is too big**
"<img src='Image1.png'" & "width='500' height='400'>" **<-- Too small. And Image doesn't load**
Sub Mail_workbook_Outlook_1()
relativepath1 = ThisWorkbook.Path & Application.PathSeparator & "Signature99" & ".png"
'Create a new email message
Set OutlookMessage = OutlookApp.CreateItem(0)
On Error Resume Next
With OutlookMessage
'.Attachments.Add "C:\ Image1.PNG", olByValue, 0
.To = Prime_Email
.cc = cc1
.BCC = ""
.Subject = "Donor Volume - " & " " & str2 & "(" & Subject & ")"
.HTMLBody = RangetoHTML(Range("E2:N16")) & "<img src ='" & relativepath1 & "'>"
.send
'.Display
End With
On Error GoTo 0
Set OutlookMessage = Nothing
Set OutlookApp = Nothing
End Sub

Related

VBA: Embedding a PNG in an Outlook Email - working for self not displaying for others [duplicate]

This question already has answers here:
Embed picture in outlook mail body excel vba
(2 answers)
Closed 1 year ago.
The below code with .PNG file referenced, works for me. However, while the .PNG gets inserted in the email, it's not actually embedded. As a result, when the recipient receives the email, the image is not displayed.
Sub Mail_workbook_Outlook_1()
relativepath1 = ThisWorkbook.Path & Application.PathSeparator & "Signature99" & ".png"
'Create a new email message
Set OutlookMessage = OutlookApp.CreateItem(0)
On Error Resume Next
With OutlookMessage
.To = Prime_Email
.cc = cc1
.BCC = ""
.Subject = "Volume - " & " " & str2 & "(" & Subject & ")"
.HTMLBody = RangetoHTML(Range("E2:N16")) & "<img src ='" & relativepath1 & "'>"
.send
'.Display
End With
On Error GoTo 0
Set OutlookMessage = Nothing
Set OutlookApp = Nothing
End Sub
First attach the image to your email, and then embed it within your email...
Sub Mail_workbook_Outlook_1()
Dim relativepath1 As String
relativepath1 = ThisWorkbook.Path & Application.PathSeparator
Dim filename As String
filename = "Signature99.png"
'Create a new email message
Set OutlookMessage = OutlookApp.CreateItem(0)
On Error Resume Next
With OutlookMessage
.To = Prime_Email
.CC = cc1
.BCC = ""
.Subject = "Volume - " & " " & str2 & "(" & Subject & ")"
.Attachments.Add relativepath1 & filename
.HTMLBody = RangetoHTML(Range("E2:N16")) & "<img src ='cid:" & filename & "'>"
.Send
'.Display
End With
On Error GoTo 0
Set OutlookMessage = Nothing
Set OutlookApp = Nothing
End Sub

Insert picture file, using variable, into text in .HTMLBody

I'm trying to use a variable to insert an image in an e-mail.
With OutlookItm
.To = strTo
.CC = strCc
.Subject = strSubject
.HTMLBody = "Hello all, <p>In attach the <b>Filtered Trunk Report</b> and below the <b>Invoice Volume chart</b>:</p>" & _
"<center><img src = Filename:ChartFile1 ></center>" & "<p>.</p>" & _
"<center><img src = 'C:\Users\matuo\OneDrive\Desktop\Chart2.jpg'></center>" & Signature
.Display
End With
The code works for "Chart2.jpg", but I want to use a variable for the directory and file.
The picture using the variable doesn't work.
Let's image you have got a string object pathToImage which contains the image path, in that case the code may look like:
With OutlookItm
.To = strTo
.CC = strCc
.Subject = strSubject
.HTMLBody = "Hello all, <p>In attach the <b>Filtered Trunk Report</b> and below the <b>Invoice Volume chart</b>:</p>" & _
"<center><img src = " & pathToImage & " ></center>" & "<p>.</p>" & _
"<center><img src = 'C:\Users\matuo\OneDrive\Desktop\Chart2.jpg'></center>" & Signature
.Display
End With
However, a local path to image is not really a good idea. Such images will not be displayed on the recipient side because the image itself is stored on your machine. Most probably you'd need to embed an image to the email itself. To get that done you need to attach the image file and set several properties - they are actually optional, even the following code works fine:
.Attachments.Add "C:\Users\JoeSchmo\Pictures\imageName.jpg", olByValue, 0
.HTMLBody = "<BODY><IMG src=""cid:imageName.jpg"" width=200> </BODY>"
But ideally you'd need to set the PR_ATTACH_CONTENT_ID and PR_ATTACHMENT_HIDDEN properties. For example:
Function SendasAttachment(fName As String)
Dim olApp As Outlook.Application
Dim olMsg As Outlook.MailItem
Dim olAtt As Outlook.Attachments
Set olApp = Outlook.Application
Set olMsg = olApp.CreateItem(0) ' email
Set olAtt = olMsg.Attachments
Const PR_ATTACH_MIME_TAG = "http://schemas.microsoft.com/mapi/proptag/0x370E001E"
Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
Const PR_ATTACHMENT_HIDDEN = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"
' attach file
olAtt.Add (fldName & fName)
Set l_Attach = olAtt.Add(fldName & fName)
Set oPA = l_Attach.PropertyAccessor
oPA.SetProperty PR_ATTACH_MIME_TAG, "image/jpeg"
oPA.SetProperty PR_ATTACH_CONTENT_ID, "myident"
oPA.SetProperty PR_ATTACHMENT_HIDDEN, True
olMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
olMsg.To = "test#email.com"
msgHTMLBody = "<HTML>" & _
"<head>" & _
"</head>" & _
"<BODY>" & "Hi " & olMsg.To & ", <br /><br /> I have attached " & fName & " as you requested." & _
"<br /><img align=baseline border=1 hspace=0 src=cid:myident width='400'/>" & _
"</BODY></HTML>"
' send message
With olMsg
.Subject = "Here's that file you wanted"
.BodyFormat = olFormatHTML
.HTMLBody = msgHTMLBody
.Save
'.Display
.Send
End With
End Function
Read more about that in the Embed Images in New Messages using a Macro article.

fixing cell padding via html in a access database

I currently have a code in a database that sends an email with a table
The table currently is not formatted it correctly and Im unable to apply cell padding. Any ideas?
enter image description here
Here is the code accompanying it
Function exporthtml()
Dim strline, strHTML, strMsg
Dim Cnt As String
Dim strFilt As String
Dim ACname As String
Dim filt As String
Dim strCC As String
Cnt = DCount("[PATS Action ID]", "tblPAT", "Bureau='" & Form_frmMainPATS.cboBur.Value & "'")
ACname = DLookup("FIRSTNAME", "qryAC", "Bureau='" & Form_frmMainPATS.txtFull.Value & "'")
strFilt = DLookup("WORKEMAIL", "qryAC", "Bureau='" & Form_frmMainPATS.txtFull.Value & "'")
Dim OL As Outlook.Application
Set OL = New Outlook.Application
Set MyItem = Outlook.Application.CreateItem(olMailItem)
Report_rptCurrentPATS.Filter = "Bureau='" & Form_frmMainPATS.cboBur.Value & "'"
Report_rptCurrentPATS.FilterOn = True
DoCmd.OutputTo acOutputReport, "rptCurrentPATS", acFormatHTML, "R:\Epi- Admin\Administrative Collaboration\Admin Review Meetings\Weekly Administrative Reports\Working Documents\Bureau Status Report Updates\TEST.html"
Open "R:\Epi-Admin\Administrative Collaboration\Admin Review Meetings\Weekly Administrative Reports\Working Documents\Bureau Status Report Updates\TEST.html" For Input As 1
Do While Not EOF(1)
Input #1, strline
strHTML = strHTML & strline
Loop
Close 1
If Left(OL.Version, 2) = "10" Then
MyItem.BodyFormat = olFormatHTML
End If
MyItem.To = strFilt
MyItem.Subject = "Updated PATS Status Report as of " & Date - 1
MyItem.HTMLBody = "<BODY bgcolor='#E6E6FA'>" & "<img src='R:\Epi-Admin\Fiscal Management and Reporting Unit\Database\PS Database\logo.png' ALT='Banner'" & "<p>" & "<FONT color = '#000000'>" & "Dear " & ACname & "," & "<br/>" & "<br/>" & Form_frmMainPATS.cboBur.Value & " currently has " & Cnt & " pending personnel actions." & "</p>" & "<p>" & "Please see the report below:" & "<br/>" & "<BODY>" & "<table border= '1'>" & "<bgcolor=#ffffff; cellspacing=10; table-layout: fixed; >" & "<table header= '1' bgcolor='#fffff'>" & strHTML & "</table>" & "</br>" & "<br/>" & "</br>" & "</br>" & "<p> If you have any questions, please contact your desingated Personnel Coordinator"
MyItem.Display
End Function
Any help would be appreciated
Expanding on my comment from above, you can try to see if something like this works or gets you in the right direction (not tested):
Dim strCell() as string
Do While Not EOF(1)
Input #1, strline
strCell() = Split(strline, vbTab) 'Replace vbTab with deliminator if needed
dim i as integer
for i = 0 to UBound(strCell)
strline = "<td>" & strCell(i) & "</td>"
next
strHTML = strHTML & "<tr>" & strline & "</tr>"
Loop
Close 1

Changing font size of the signature of an outlook email generated through VBA

i'm able to make the signature bold but i cant change the size. I can also change the size of the body but not the signature. I need to change the font size of the signature to match the font size of the body in the email.
Sub Email_Test()
'Exit function if user input incomplete:
If IsNull(Forms!frmCompMain!cboPayPrd) = True Then
MsgBox "Please provide the Pay Period parameter!", vbCritical
Exit Sub
End If
'-----------------------------------------
----'DECLARE AND SET VARIABLES
Dim myOutlok As Object
Dim myMailItm As Object
Dim Signature As String
Dim OtlApp As Object
Dim OtlNewMail As Object
Dim olMailItem As Object
Dim PayPrd As String
Set OtlApp = CreateObject("Outlook.Application")
Set OtlNewMail = OtlApp.CreateItem(0)
PayPrd = Forms!frmCompMain!cboPayPrd
'-----------------------------------------
-----'GET DEFAULT EMAIL SIGNATURE
Signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(Signature, vbDirectory) <> vbNullString Then
Signature = Signature & Dir$(Signature & "*.htm")
Else:
Signature = ""
End If
Signature =
CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll
'-----------------------------------------
----'CREATE EMAIL
OtlNewMail.HTMLBody = Signature
With OtlNewMail
.to = ""
.CC = ""
.Subject = ""
.HTMLBody = "<font size='2'> Hello," & "<br />" & _
"<br />" & _
"" & "<br />" & _
"<br />" & _
"<b>Production Period:</b> " & DateSerial(Year(PayPrd)" & _
"<br />" & _
"<b> Pay Date:</b> " & DateSerial(Year(PayPrd), Month(PayPrd) + 1, 10) &
"<br />" & _
"<br />" & _
"Please let me know if you have any questions." & "<br />" & _
"<br />" & _
"<b>" & Signature & "</b>"
.display
'.Send
End With
'-----------------------------------------
----'CLEANUP
End Sub
Firstly, you cannot concatenate two HTML strings and expect a valid HTML string back. The two must be merged.
Secondly, if the font size is explicitly set in the HTML signature, your code explicitly wrapping the signature in an element with a specified font size would do nothing. You'd need to use the HTMLDocument interface or the Word object model to set the size.
Or, the simplest possible solution, make sure the stationary signature already has the right font.
You should use the following code replace the .HTMLBody content:
Signature = "<b style='color:red;'>Your Sigature</b>"
With OutMail
.To = ""
.Subject = "This is the Subject line"
.HTMLBody = "<font style='font-size:20px !important;'> Hello <br /><br/><br/><b>Production Period:</b> DateSerial(Year(PayPrd)<br /><b> Pay Date:</b> DateSerial(Year(PayPrd), Month(PayPrd) + 1, 10)<br/><br/>Please let me know if you have any questions.<br/><br/><b style='font-size 14px !important'>" & Signature & "</b></font>"
.Display
If the font size is set in the HTML signature, You'd need to use the HTMLDocument interface to set the size.

VBA & HTMLBody - Spacing between Body and Signature

I will be using excel to send emails to my clients requesting certain files from them. I have everything working except for 1 small detail and I do not want to use this until I have that 1 detail figured out.
My email populates almost perfectly, except for the fact that at the end, there is about 3 lines of space between "Regards" and my signature. I'm not sure why this is happening. It shows up like this:
Thank you for your attention in this matter.
Regards,
Signature
Does anyone know how to fix it. My code is listed below:
Sub KYC_FATCA()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim signature As String
Dim AccOpen As String
Dim ConDoc As String
Dim SIP As String
Dim AFS As String
Dim W8 As String
Dim LEI As String
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("G").Cells.SpecialCells(xlCellTypeConstants)
'KYC Account Opening Form
If (Cells(cell.Row, "I").Value) = "No" Then
AccOpen = "<b>KYC Account Opening Form</b> ." & "<br>" & "<br>"
Else
AccOpen = ""
End If
'Constating Document
If (Cells(cell.Row, "J").Value) = "No" Then
ConDoc = "<b>Constating Document</b> - ." & "<br>" & "<br>"
Else
ConDoc = ""
End If
'Statement of Policy and Guidelines (SIP&G)
If (Cells(cell.Row, "L").Value) = "No" Then
SIP = "<b>Statement of Policy and Guidelines (SIP&G)</b> - " & "<br>" & "<br>"
Else
SIP = ""
End If
'Audited Financial Statements (AFS)
If (Cells(cell.Row, "M").Value) = "No" Then
AFS = "<b>Audited Financial Statements (AFS)</b> - ." & "<br>" & "<br>"
Else
AFS = ""
End If
'W-8BEN-E Form
If (Cells(cell.Row, "N").Value) = "No" Then
W8 = "<b>W-8BEN-E Form</b> - " & "<br>" & "<br>"
Else
W8 = ""
End If
'Legal Entity Identifier (LEI)
If (Cells(cell.Row, "O").Value) = "Needed" Then
LEI = "<b>Legal Entity Identifier (LEI)</b> - " & "<br>" & "<br>"
Else
LEI = ""
End If
If cell.Value Like "?*#?*.?*" And _
(Cells(cell.Row, "H").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
End With
signature = OutMail.HTMLbody
On Error Resume Next
With OutMail
.To = cell.Text 'Whatever is in cell G
.cc = Cells(cell.Row, "C").Value
'Testing if statements - The below one works perfect
'If LCase(Cells(cell.Row, "Z").Value) = "" Then
' .cc = Cells(cell.Row, "P").Value
'End If
.Subject = Cells(cell.Row, "A").Value & " - " & "Documentation Request" _
.HTMLbody = "<p style='font-family:calibri;font-size:11pt'>" & "Dear " & Cells(cell.Row, "D").Value & ",<br>" & "<br>" & _
"On behalf of " & Cells(cell.Row, "B").Value & ", please by " & "<b><u>" & Cells(cell.Row, "Q").Text & "</b></u>" & "." & "<br>" & "<br>" & _
AccOpen & _
ConDoc & _
SIP & _
AFS & _
W8 & _
LEI & _
"If you have any questions and/or concerns, please contract your Relationship Manager, " & Cells(cell.Row, "B").Value & "." & "<br>" & "<br>" & _
"Thank you for your attention in this matter." & "<br>" & "<br>" & _
"Regards," & "</p>" & _
signature _
'You can add files also like this
If (Cells(cell.Row, "I").Value) = "No" Then
.Attachments.Add ("C:doc")
End If
.Display 'This will open the message itself. If you'd like to send right away, use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
The problem is this line:
signature = OutMail.HTMLbody
This is a clever way to get the signature, but the default email body has a couple blank lines above the signature, and those are getting included when you concatenate the email.
I would visually check signature in a debugger and see what is in there, and remove the stuff you don't want. A simple example might be:
Function RemoveBlankStuff(ByVal text as string) as string
text = text.Replace("<P></P>","") 'Remove any empty paragraphs
text = text.Replace("<BR>","") 'Remove any line breaks
Return text;
End Function
signature = RemoveBlankStuff(OutMail.HTMLBody);
You will need to modify the above function depending on what you find in signature.
I was doing something similar and running into the same issue.
This should work if you replace this portion of code:
With OutMail
.Display
End With
signature = OutMail.HTMLbody
With the following code - it effectively opens, deletes whitespace, and deletes/discards the e-mail created to get the signature:
'gets default signature from e-mail
With OutMail
'2 = HTMLBody
.BodyFormat = 2
.Display
'deletes blank space present before signature
signature = Replace(OutMail.HTMLBody, "<p class=MsoNormal><o:p> </o:p></p>", "")
'removes entire e-mail contents and then closes with discard
OutMail.HTMLBody = Replace(OutMail.HTMLBody, OutMail.HTMLBody, "")
OutMail.Close 1
End With
Not sure if it makes a difference, but in .HTMLBody, I also start as -
.HTMLBody = .HTMLBody & "Good Afternoon," & "<br>" & "whateveryourtextis" & "Thanks," & "<br><br>" & signature