Email body format changed for some recipients VBA - vba

I am creating some emails through vba in excel. Currently using the html method for the email body and adding the mail body text by combining values of different cells in a worksheet. I clearly marked the font as Calibri 11 and it is creating the mails with the required font but some users get the mail with different font in email body.
Also, the recipients who are facing this problem have the "Dear" part and "Thank you " part showing as Calibri 11 but the rest of the content in Times New Roman 12. Can someone please help me with this issue.
The code for email body is like this:
.htmlbody = "<body style = 'font-size:11pt; font-family:Calibri; color:RGB(0,0,0);'>" & y & vbNewLine & _
"<p>" & varPart1 & varPart2 & "<sup>" & varPart3 & "</sup>" & varPart4 & varPart5 & "<sup>" & _
varPart6 & "</sup>" & varPart7 & "</p>" & _
"<p>" & varPart8 & "</p>" & _
"<p><span>" & varPart9 & "</span></p>" & _
"<span>" & varPart10 & "</body>"

Related

For Loop to create a dynamic outlook email body that will have lines dependent on trades for a specific date

I am generating an email that shows trades for the day in the body of an email. Currently the only way I can do this is with an If then statement for specific numbers of trades. If we traded 10, I need an if then statement with 10 as the variable criteria, but if I only have 9, then I get an error. I want a dynamic method instead. I can do a For Loop that will list all the trades in debug.print, but in the email, it each trade overwrites the prior trade and I show only one line. I also need an intro like "Hi, today today's trades are: " followed by each trade listed below line by line.
I tried this and it works but only if I have the right number of trades and a variable that matches it. For example, if I had 23 trades, I need an If statement with 23 as a variable value in this case m. I used Arrays as VBA does not let me create a list. Unfortunately, I cannot pull the whole array, I need to pull line by line. If I could pull the array, I could just have a simple loop.
If m = 23 Then
.Body = "Hi Chris," & vbLf & vbLf & "The following trade(s) was completed today:" & vbLf & vbLf & ArrayValues(0) & vbLf & ArrayValues(1) & vbLf & ArrayValues(2) & vbLf & ArrayValues(3) & vbLf & ArrayValues(4) & vbLf & ArrayValues(5) & vbLf & ArrayValues(6) & vbLf & ArrayValues(7) & vbLf & ArrayValues(8) & vbLf & ArrayValues(9) & vbLf & ArrayValues(10) & vbLf & ArrayValues(11) & vbLf & ArrayValues(12) & vbLf & ArrayValues(13) & vbLf & ArrayValues(14) & vbLf & ArrayValues(15) & vbLf & ArrayValues(16) & vbLf & ArrayValues(17) & vbLf & ArrayValues(18) & vbLf & ArrayValues(19) & vbLf & ArrayValues(20) & vbLf & ArrayValues(21) & vbLf & ArrayValues(22) & vbLf & vbLf & "Thanks"
End if
I want to use something like:
For b = 1 To LastRow
If Trades.Range("H" & b) = TDate Then
Debug.Print (Range("B" & b) & " " & Range("C" & b) & " " & Range("D" & b) & " " & Range("F" & b) & " " & Range("G" & b))
End If
Next b
This way it does not matter how may trades I have, one formula would over it all. Each Range has a trade characteristic.
If I do a debug.Print in the immediate window I get a list just like I want, but in the email, each line overwrites the prior trade.
I am a rookie at this and appreciate any help. Thanks
Sounds like you need to build a string which can be assigned to the message body. For such tasks you can use String Functions available in VBA where you can prepare the correct string and then assign to the message body.
If m = 23 Then
.Body = "Hi Chris," & vbLf & vbLf & "The following trade(s) was completed today:" & vbLf & vbLf & ArrayValues(0) & vbLf & ArrayValues(1) & vbLf & ArrayValues(2) & vbLf & ArrayValues(3) & vbLf & ArrayValues(4) & vbLf & ArrayValues(5) & vbLf & ArrayValues(6) & vbLf & ArrayValues(7) & vbLf & ArrayValues(8) & vbLf & ArrayValues(9) & vbLf & ArrayValues(10) & vbLf & ArrayValues(11) & vbLf & ArrayValues(12) & vbLf & ArrayValues(13) & vbLf & ArrayValues(14) & vbLf & ArrayValues(15) & vbLf & ArrayValues(16) & vbLf & ArrayValues(17) & vbLf & ArrayValues(18) & vbLf & ArrayValues(19) & vbLf & ArrayValues(20) & vbLf & ArrayValues(21) & vbLf & ArrayValues(22) & vbLf & vbLf & "Thanks"
End if
Your strategical mistake is that you are trying to set up the Body property for a specific case. Instead, consider concatenating the string with the required piece of data (another string) which makes sense for a particular case. And only when you are done adding all the bits to the result string, you can assign it to the message body.
Be aware, the Body property is a plain text string and doesn't deliver any formatting. Instead, I'd suggest using the HTMLBody property instead. The HTMLBody property should be an HTML syntax string. Setting the HTMLBody property will always update the Body property immediately.

adding tab or space before the bulletpoints in my email body

I want to add space before the Chr(149) to make the email look more visually pleasing. Could you guys help me?
My current VBA code is the following:
Sub EmailHyperlink()
'updated by Extendoffice 20190815
Dim xOtl As Object
Dim xOtlMail As Object
Dim xStrBody As String
xStrBody = "Hi [Name]" & "<br>" _
& " " & "<br>" _
& "we receives monthly traffic of 4,000 visitors looking for video services, and we can help [Agency Name] get more qualified inquiries." & "<br>" _
& " " & "<br>" _
& "We created a USD -worth of promotion package available for USD only until August 17, 2022, which includes placements in:" & "<br>" _
& " " & "<br>" _
& Chr(149) & "Featured Video Design - visitors, avg. session time." & "<br>" _
& Chr(149) & " Top Video Production Companies - visitors, avg. session time." & "<br>" _
& Chr(149) & " Top Video Marketing Agencies - visitors, avg. session time." & "<br>" _
& Chr(149) & " 10 Best Video Commercials - visitors, avg. session time." & "<br>" _
& " " & "<br>" _
& "Slots are limited and secured on a first-come, first-served basis." & "<br>" _
& "<br>" _
& "If interested, kindly reply to this email or book a meeting with me." & "<br>" _
& " " & "<br>" _
& " " & "<br>" _
& "Thank you."
On Error Resume Next
Set xOtl = CreateObject("Outlook.Application")
Set xOtlMail = xOtl.CreateItem(olMailItem)
With xOtlMail
.To = "Email Address"
.CC = " "
.Subject = "Would [Agency Name] want more exposure for its video services? "
.HTMLBody = .HTMLBody & xStrBody
.Display
End With
Set xOtl = Nothing
Set xOtlMail = Nothing
End Sub
i see you use <A HREF...>, suppose you could use unordered list with the <ul>-tag.
Here's an example from w3schools HTML Lists:
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Coffee
Tea
Milk
Do you mean vertical space or horizontal?
For vertical space, instead of using <br> it is easier to control spacing using <p>. So you might use <p style="margin:1em 0;">, or instead of 1em, 20px.
For horizontal space, you can add a bit more spacing by using a non-breaking space. So that will look like this: " ". That is, space, non-breaking space, space: and this will achieve three spaces. (Multiple spaces without the non-breaking space obviously only achieve one space.)

How to insert enters into email body in Outlook

What I'm trying to do is to copy a range from Excel (2 columns) and to paste in into email body in Outlook. I'd like to get also 2 columns in email body but I still receive everything in one line. Here's my code:
Tresc = "Cześć" & vbCr & vbCr & " słówko/a na dzisiaj to:" & vbCr & vbCr & Tresc & vbNewLine
'utworzenie hiperłącza
Hiperlacze = "mailto:" & Trim(LCase(Adres)) & _
"?subject=" & Tytul & _
"&body=" & Tresc
I believe you are looking for the vbCRLF built-in for "carriage return/linefeed" instead of just vbCR.
Since this is a mailto-link, there are some other rules in play. Replace the vbCr with "%0D%0A".
Example:
Tresc = "Cześć%0D%0Asłówko/a na dzisiaj to:" 'etc.

vba add more than one space in vba email body?

I am using vba in outlook to generate an email. I would like to find a way to move some text over to the right, about 100 pixels.
since I don't believe there is a way to incorporate css or styles in vba I am looking for a way to add more than one space to get the text moved over. however when I try space() function and " ", even if I repeat these codes a number of times it only ever gives me one space.
can someone please help me and show me what I need to do, thanks
"<br><br><br>" & "3PL & HAULAGE SUPPLIERS: " & " " & "<font size=""4.5"" face=""calibri"" color=""red"">" & "<b>" & EmailCount & "</b></font>" & vbNewLine & _
This will add 10 white spaces just before 3PL. You may need to adjust as the pixel distance will be relative to the font
TRIED AND TESTED
Sub test()
Dim WS As String
WS = "&nbsp"
For i = 1 To 10
WS = WS & "&nbsp"
Next i
Debug.Print "<br><br><br>" & WS & "3PL & HAULAGE SUPPLIERS: " & " " & "<font size=""4.5"" face=""calibri"" color=""red"">" & "<b>" & EmailCount & "</b></font>" & vbNewLine
End Sub

Specifying the location of an inlineshape in MS Word (VBA)

I've been trying to adapt the method shown here: http://support.microsoft.com/kb/246299 so that I can create a command button in word which will save the document and remove itself when clicked. I've been unable to figure out how to change the position of the button from the default of the top left of the first page however. Ideally I'd like the button to be generated at the end of the document and be centre aligned, or otherwise placed at the cursor position.
Any advice would be very much appreciated :)
Thank You.
My VB.NET project code so far:
Dim shp As Word.InlineShape
shp = wrdDoc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Save To Disk"
shp.Width = "100"
'Add a procedure for the click event of the inlineshape
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
"ActiveDocument.SaveAs(""" & sOutFile & """)" & vbCrLf & _
"On Error GoTo NoSave" & vbCrLf & _
"MsgBox ""Document Saved Successfully""" & vbCrLf & _
"Dim o As Object" & vbCrLf & _
"For Each o In ActiveDocument.InlineShapes" & vbCrLf & _
"If o.OLEFormat.Object.Name = ""CommandButton1"" Then" & vbCrLf & _
"o.Delete" & vbCrLf & _
"End If" & vbCrLf & _
"Next" & vbCrLf & _
"Exit Sub" & vbCrLf & _
"NoSave:" & vbCrLf & _
"MsgBox ""Document Failed To Save""" & vbCrLf & _
"End Sub"
wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)
wrdApp.Visible = True
As long as everything else is working just set the shp.left = 250 and shp.top = 1200
etc etc.
Just like in VB when you place a button. For more details on the exact calls you should reference this page: http://msdn.microsoft.com/en-us/library/office/hh965406%28v=office.14%29.aspx
But say to center a button you can set left to be the (doc.width - shape.width)
But word buttons allow for much more complex styling and setup.