在 VBA 中更改 HTML 电子邮件正文字体类型和大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22200910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change HTML email body font type and size in VBA
提问by
I have a VBA script that that generates and email when a VBA button is pushed in a given worksheet.
我有一个 VBA 脚本,当在给定的工作表中按下 VBA 按钮时,它会生成和发送电子邮件。
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.
该脚本当前以相对较小的字体生成电子邮件。我想知道是否有办法将字体设置为 Calibri,并将文本设置为 11。
Here is the current VBA script:
这是当前的 VBA 脚本:
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?
是与电子邮件正文文本大小相关的部分。但是3的设置太小了,4的设置太大了。所以我只是想知道是否有某种方法可以将字体设置为 11 号,并将文本设置为 Calibri 格式?
Thank you!
谢谢!
回答by
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.
显然通过设置"font-size=11pt"
而不是设置字体大小<font size=5>
,它允许您像通常在文本编辑器中一样选择特定的字体大小,而不是像我的代码最初那样从 1-7 中选择一个值。
This linkfrom simpLE MAn gave me some good info.
这个来自 simple man 的链接给了我一些很好的信息。
回答by Seb
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 :
仅供参考,我也做了一些研究,如果您要应用的字体系列的名称包含空格(例如我以Gill Alt One MT Light为例),您应该这样写:
strbody= "<BODY style=" & Chr(34) & "font-family:Gill Alt One MT Light" & Chr(34) & ">" & YOUR_TEXT & "</BODY>"
回答by Günter
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