【问题标题】:Change font and size of strbody element which is referencing an excel cell?更改引用 Excel 单元格的 strbody 元素的字体和大小?
【发布时间】:2020-06-25 07:28:42
【问题描述】:

我知道我的代码很笨拙,我正在尽我所能。

背后的想法是拥有一个包含所有相关详细信息的 Excel 表格,并根据此表格发送包含相应相关内容的电子邮件。

电子邮件以 strbody = Cells(cell.Row, "A").Value & Cells(cell.Row, "B").Value _

开头

这里的 cell.row A 是问候语(Dear Sir 或 Dear Madam),cell.row B 是人名。

如果我运行代码,一切正常,但该代码行的字体被弄乱了。它以新罗马时代显示,但我希望它具有 arial 大小:10pt。我尝试了一切,但总是出错。

有什么想法吗?

提前致谢。

Sub test1()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strbody As String
Application.ScreenUpdating = False

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error GoTo cleanup



For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
       LCase(Cells(cell.Row, "D").Value) = "ENG" Then
        On Error Resume Next

       With OutMail
        .display

    strbody = Cells(cell.Row, "A").Value & Cells(cell.Row, "B").Value _
    & "<p style= font-family:arial;font-size:10pt> Welcome </p>" _
    & IIf(Cells(cell.Row, "G").Value = "incomplete", "<p style= font-family:arial;font-size:10pt>Please do not forget to complete your registration:<p/> " & Cells(cell.Row, "F").Value, "<p> <p/>") _
    & "<h3  style= font-family:arial;font-size:11pt><font color=#5b9bd5><u>Check-in & Check-out</u></font></h3>" _
    & "<p style= font-family:arial;font-size:10pt>Check-In: <b>ab 15:00 Uhr</b> & Check-out: <b>bis 10:00 Uhr</b> Other hours on request </p>" _
    & "<p style= font-family:arial;font-size:10pt>Thanks</b></p>" _

            .Attachments.Add ("G:\E-Mail Vorlagen\Anhang\Anreise Infos\Nützliche Informationen.pdf")


            .To = Cells(cell.Row, "C").Value
            .Subject = "Your arrival" & Cells(cell.Row, "E").Value
            .htmlbody = strbody & .htmlbody
            .BodyFormat = olFormatHTML


        End With

        On Error GoTo 0
        Set OutMail = Nothing
    End If
Next cell


cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 你为什么不改变单元格中的字体而不是使用HTML标签?
  • 这就是我试图做的(在 Excel 和 Outlook 中),但它不起作用。

标签: excel vba outlook


【解决方案1】:

使用单引号(即 ticks )来隔离您的样式命令。

...
& "<p style='font-family:arial;font-size:10pt;'> Welcome </p>"
...

【讨论】:

  • 感谢您的建议。我试过了,但不幸的是

    开头和结尾的引号导致单元格引用的引号出现问题。见这里:strbody = "

    Cells(cell.Row, "A").Value & Cells(cell.Row, "B").Value

    "_我也试过在开头和结尾加双引号"""",还是不行。。
猜你喜欢
  • 1970-01-01
  • 2013-02-03
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2011-03-20
  • 1970-01-01
  • 2011-07-16
相关资源
最近更新 更多