Adding TAB to the beginning of a text string in Excel VBA - excel

I've assembled the following code that creates the following text in the body of an Outlook email when run.
Sub InitialEvaluationWalkThru()
Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strto = "first.last#me.com"
strcc = ""
strbcc = ""
strsub = [$C$8] & " Update"
strbody = "<H5>Hello Everyone,</H5>" & _
"<H5>The Template conference rooms " & Cells(FormulaCell.Row, "C").Value & " phase is now complete!</H5>" & _
"<H4>The overall room deployment is " & [$E$8] & "% Complete!</H4>" & _
"<H5>Let me know if you have any questions,</H5>"
With OutMail
.Display
.To = strto
.CC = strcc
.BCC = strbcc
.Subject = strsub
' .Body = strbody
.HTMLBody = strbody & .HTMLBody
'You can add a file to the mail like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Outlook Email
Hello Everyone,
The Template conference rooms Initial Evaluation Walk-Thru phase is
now complete!
The overall room deployment is 0.125% Complete!
Let me know if you have any questions,
Question
I am trying to indent the third line and have tried many different combos with no luck. I've also tried to have the cell value percentage from my sheet as 13% not 0.125%, also with no luck.
Any help would be greatly appreciated.
Thank You

For the value, you should be able to simply multiply it by 100:
"<H4>The overall room deployment is " & CInt(100 * [$E$8]) & "% Complete!</H4>" &
CInt will round the value up or down. In your case it should round up 12.5 to 13.
As far as the tab, I would try what #JNevill suggests and use or   (see Special Characters) but put it in between <pre></pre> tags. See the many approaches discussed in this question: How to get a tab character?.

Related

How to include signature on email?

I have a code that sends an email too individuals on a list.
I need the user's signature to appear at the bottom of the email. I cannot get it to display.
Below is my code.
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo Cleanup
For Each cell In Columns("M").Cells.SpecialCells(xlCellTypeConstants)
If LCase(Cells(cell.Row, "M").Value) = "no" Then
Set OutMail = OutApp.CreateItem(0)
strbody = "Dear " & Cells(cell.Row, "A").Value _
& "<br>" & "<br>" & _
"You still have outstanding work on the Rescan Spreadsheet " & _
" Title number: " & Cells(cell.Row, "E").Value _
& "<br>" & "<br>" _
& "<A href=""\\cv-vfl-d01\dlr_office\Operational Teams\RR Scanning Team\" & _
"Back file QA Xerox\Document Rescans\Rescans 2019"">Click here to open file location</A>"
On Error Resume Next
With OutMail
.To = Cells(cell.Row, "B").Value
.CC = "Bethany.Turner#Landregistry.Gov.uk"
.Subject = "Re-Scan Reminder"
.HTMLBody = strbody & .HTMLBody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
Cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
MsgBox "Reminder Sent", vbOKOnly
End Sub
Your code needs to call Display first - that is when Outlook inserts the default signature into an empty email.
Secondly, do not concatenate two HTML strings - they must be merged, not concatenated: in the simplest case, search for the position of the "<body" substring, find the next occurrence of the ">" character (that takes care of the "<body>" HTML elements with attributes), then insert your HTML text.
If you want to send a message without displaying it first or if you want to insert an arbitrary signature, you can use Redemption (I am its author) and its RDOSignature.ApplyTo method.

How to send an Excel sheet to Outlook mailbox?

I have an Excel document to log work done during the day to be passed to the nightshift so they are kept up-to-date with the days activites and vice versa.
The plan is to fill out the document and click a 'Send' button that will send the newly created Excel sheet to a shared Outlook folder.
My attempts have been scrapes off the web copied & tried, but to no avail.
Maybe it may help :)
Sub outMail()
Dim outApp As Object
Dim oMail As Object
Dim signature As String
Dim obszar As String
Set outApp = CreateObject("Outlook.Application")
Set oMail = outApp.CreateItem(0)
With oMail
.Display
End With
signature = oMail.Body
With oMail
.To = "email#email.com"
.CC = "email2#email.com"
.BCC = ""
.Subject = "Log work done during the day"
.BodyFormat = 2
.Body = "Hello" & Chr(13) & Chr(10) & "The newly created Excel sheet with log work done during the day " & Chr(13) & Chr(10) & signature
'here You put directory to your file, for now its directory to file where macro is
.Attachments.Add ActiveWorkbook.FullName
'now its set to display only, if You want to send automatically put .send as below
.Display
'.Send
End With
End Sub
If you use HTML for the email body, you will also be able to format the mail body. Just change the file path to attach the mail. If you want to include a new line in the mail body use < br > (without space).
Sub StackOverflow()
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = "Johnston#Stackoverflow.com"
.Subject = "Excel Document"
.Display
.HTMLBody = "<p style='font-family:arial;font-size:13'>" & _
"Hi" & "<br>" & "<br>" & _
"Here is the Excel document." & _
.HTMLBody
.Attachments.Add ("C:\Desktop\" & "ExcelDocument.xlsx")
.Display
End With
End Sub

How insert hyperlink from a column into Outlook email body

I have column "AB" that has a hyperlink in which I will like to include in a email through VBA.
The hyperlink changes per line. I am able to pull the column through text however the email is not showing the hyper link.
How can I get it to show as a hyperlink?
Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strto = Cells(FormulaCell.Row, "Y").Value
strcc = ""
strbcc = ""
strsub = "MCR FORM"
strbody = "Hi " & Cells(FormulaCell.Row, "O").Value & vbNewLine & vbNewLine & _
"You have a open MCR that needs attention. Please Find the attachted MCR Form for material : " & Cells(FormulaCell.Row, "E").Value & _
vbNewLine & vbNewLine & Cells(FormulaCell.Row, "AB").Value & vbNewLine & vbNewLine & "Thank you!"
With OutMail
.To = strto
.CC = strcc
.BCC = strbcc
.Subject = strsub
.Body = strbody
'You can add a file to the mail like this
.Attachments.Add ("P:\Inventory Control\Public\MCR Form Master.xlsm")
.Display ' or use .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
vbNewLine & vbNewLine & Cells(FormulaCell.Row, "AB").Value &
I believe the code above needs to be reference a HREF link??
Work with HTMLBody Property
Example will show Hyperlink Click Here
.HTMLBody = " Click Here "
Or This will show the value A1 as link
"" & Sht.Range("A1") & "" &
Full Code
Option Explicit
Public Sub Example()
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.Worksheets("Sheet1")
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.HTMLBody = "Hi " & vbNewLine & vbNewLine & _
"You have a open MCR that needs attention. " & _
vbNewLine & vbNewLine & _
" Click Here " & _
vbNewLine & vbNewLine & _
"Thank you!"
'You can add a file to the mail like this
.Display ' or use .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Option Explicit Statement (Visual Basic)
Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.

How to attach active Excel workbook to an email

I have been trying all morning to get this VBA script to attach my active excel document to an auto-generated outlook message. Everything works fine if I declare the file path as a string and attach it. Except that I would like to attach the full file path of the current excel document instead of using a static string value.
Here is my code:
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim sAttach As String
Dim sTo As String
Dim sCC As String
'For To field
Set emailRng = Worksheets("Pre-Clearance Email").Range("E11:J14")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
'For CC field
Set emailRngCC = Worksheets("Pre-Clearance Email").Range("E16:J19")
For Each cl In emailRngCC
sCC = sCC & ";" & cl.Value
Next
sCC = Mid(sCC, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'variable declarations for email body and attachment
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>Please see the attached aliases for validation. Please let me know if you have any questions.<p>Thank you.</BODY>"
sAttach = "K:\CRM Support\Data\Systematic Trade Recon (1).xlsm"
'the below code adds a users default signature to the email
With OutMail
.Display
End With
signature = OutMail.HTMLBody
With OutMail
.to = sTo
.CC = sCC
.Subject = "STR Pre-Clearance"
.HTMLBody = strbody & signature
.Attachments.Add (ActiveDocument.FullName)
'.Attachments.Add sAttach
.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
The compiler gives me an error at this line:
.Attachments.Add (ActiveDocument.FullName)
I have done some research, and tried to fix the problem myself, but I just can't figure out how to make this script attach the active file to this outlook message. As you can see by my code, my backup option is to just use a string variable and a static address to attach the file, but I would rather make this script more versatile than that.
Here is one of the sites which I found that gave me this idea to begin with: Here
Well, after some more effort I was able to get the workbook to attach flawlessly. Here was the revision I made to the OutMail Object in my orginial code:
With OutMail
.to = sTo
.CC = sCC
.Subject = "STR Pre-Clearance"
.HTMLBody = strbody & signature
.Attachments.Add (ActiveDocument.FullName) 'this is the correction I made
.Display
I figured I would answer my own question so it doesn't linger without a technical answer. Maybe it will help someone in the future.
The fix should actually be:
With OutMail
.To = sTo
.CC = CC
.Subject = "STR Pre-Clearance"
.HTMLBody = strbody & signature
.Attachments.Add (ActiveWorkbook.FullName) 'should be workbook not document
.Display 'or .Send

Change HTML email body font type and size in VBA

I have a VBA script that that generates and email when a VBA button is pushed in a given worksheet.
The script currently generates the email in a relatively small font. I was wondering if there is a way to set the font to Calibri, and the text sive to exactly 11.
Here is the current VBA script:
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim strUser As String
Dim signature As String
Dim sTo As String
Dim sCC As String
'For To field
Set emailRng = Worksheets("Send Email").Range("D3:I6")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
'For CC field
Set emailRngCC = Worksheets("Send Email").Range("D8:I11")
For Each cl In emailRngCC
sCC = sCC & ";" & cl.Value
Next
sCC = Mid(sCC, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
End With
signature = OutMail.HTMLBody
strbody = "<FONT SIZE = 3>Good Morning;<p>We have completed our main aliasing process for today. All assigned firms are complete. Please feel free to respond with any questions.<p>Thank you."
With OutMail
.SentOnBehalfOfName = ""
.To = sTo
.CC = sCC
.BCC = ""
.Subject = "Data Morning Alias Process - COMPLETE"
.HTMLBody = strbody & signature
.Display
End With
End Sub
I know that this portion of the code:
strbody = "<FONT SIZE = 3.5>Good Morning;<p>We have completed our main aliasing
is the portion that relates to the email body text size. But a setting of 3 is to small, and a setting of 4 is too big. So I was just wondering if there is some way I can set the font to be exactly size 11, and the text to be formatted as Calibri?
Thank you!
I did a little research and was able to write this code:
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>We have completed our main aliasing process for today. All assigned firms are complete. Please feel free to respond with any questions.<p>Thank you.</BODY>"
apparently by setting the "font-size=11pt" instead of setting the font size <font size=5>,
It allows you to select a specific font size like you normally would in a text editor, as opposed to selecting a value from 1-7 like my code was originally.
This link from simpLE MAn gave me some good info.
FYI I did a little research as well and if the name of the font-family you want to apply contains spaces (as an example I take Gill Alt One MT Light), you should write it this way :
strbody= "<BODY style=" & Chr(34) & "font-family:Gill Alt One MT Light" & Chr(34) & ">" & YOUR_TEXT & "</BODY>"
Set texts with different sizes and styles, and size and style for texts from cells ( with Range)
Sub EmailManuellAbsenden()
Dim ghApp As Object
Dim ghOldBody As String
Dim ghNewBody As String
Set ghApp = CreateObject("Outlook.Application")
With ghApp.CreateItem(0)
.To = Range("B2")
.CC = Range("B3")
.Subject = Range("B4")
.GetInspector.Display
ghOldBody = .htmlBody
ghNewBody = "<font style=""font-family: Calibri; font-size: 11pt;""/font>" & _
"<font style=""font-family: Arial; font-size: 14pt;"">Arial Text 14</font>" & _
Range("B5") & "<br>" & _
Range("B6") & "<br>" & _
"<font style=""font-family: Chiller; font-size: 21pt;"">Ciller 21</font>" &
Range("B5")
.htmlBody = ghNewBody & ghOldBody
End With
End Sub
'Fill B2 to B6 with some letters for testing
'"<font style=""font-family: Calibri; font-size: 15pt;""/font>" = works for all Range Objekts

Resources