【问题标题】:keep format of text in cell in html body将文本格式保留在 html 正文中的单元格中
【发布时间】:2014-06-25 13:18:32
【问题描述】:

我有一个简单的问题:

在 emailSender.Cells(2, 5) 中存储了我想在电子邮件的 html 正文中使用的文本,但我需要在此单元格中保持准确的格式,我该怎么做?

With MItem
        .To = EmailAddr
        .Subject = Subj
            Do While j <= lastRow
                Set searchResult3 = report.Columns(1).Find(tickets.Cells(j, i), LookIn:=xlValues)
                .HTMLBody = .HTMLBody & "<tr>" & "<td width=""120"">" & tickets.Cells(j, i) & "</td>" & "<td width=""250"">" & searchResult3.Offset(0, 3) & "</td>" & "</tr>"
                j = j + 1
            Loop
        .HTMLBody = emailSender.Cells(2, 5) & "<table border=""1"" bordercolor=black style=""border-collapse: collapse"">" & .HTMLBody & "</table>" & "<br>" & signature
        .Display
     End With

感谢您的帮助:)

【问题讨论】:

  • 将单元格格式化为 TEXT 或在单元格中的所有内容前加上单引号 '
  • 它无论如何都没有帮助 :) 这个单元格中的一些文本是粗体的,有些不是,在 html 正文中我需要它看起来与那个单元格中的一样
  • 所以你想评估 HTML??
  • 是的,我认为这可能是一种方式.. 可以说这个单元格 emailSender.Cells(2, 5) 中的文本是“Arial”,大小为 10,所以当我使用这个 .HTMLBody = emailSender.Cells(2, 5).... 我需要它使用 Arial 和大小 10,而不是 Outlook 的默认值
  • 哦,这几乎是不可能的:P

标签: excel vba outlook


【解决方案1】:

我用谷歌搜索了“excel vba 将单元格转换为 html”,第一个结果是一个完整的解决方案。

浏览这段代码,我可以看到它有粗体、斜体、下划线和字体颜色。如果你需要的话,你应该能够轻松地调整字体颜色代码来调整字体大小。

Function fnConvert2HTML(myCell As Range) As String
Dim bldTagOn, itlTagOn, ulnTagOn, colTagOn As Boolean
Dim i, chrCount As Integer
Dim chrCol, chrLastCol, htmlTxt As String

bldTagOn = False
itlTagOn = False
ulnTagOn = False
colTagOn = False
chrCol = "NONE"
htmlTxt = "<html>"
chrCount = myCell.Characters.Count

For i = 1 To chrCount
    With myCell.Characters(i, 1)
        If (.Font.Color) Then
            chrCol = fnGetCol(.Font.Color)
            If Not colTagOn Then
                htmlTxt = htmlTxt & "<font color=#" & chrCol & ">"
                colTagOn = True
            Else
                If chrCol <> chrLastCol Then htmlTxt = htmlTxt & "</font><font color=#" & chrCol & ">"
            End If
        Else
            chrCol = "NONE"
            If colTagOn Then
                htmlTxt = htmlTxt & "</font>"
                colTagOn = False
            End If
        End If
        chrLastCol = chrCol

        If .Font.Bold = True Then
            If Not bldTagOn Then
                htmlTxt = htmlTxt & "<b>"
                bldTagOn = True
            End If
        Else
            If bldTagOn Then
                htmlTxt = htmlTxt & "</b>"
                bldTagOn = False
            End If
        End If

        If .Font.Italic = True Then
            If Not itlTagOn Then
                htmlTxt = htmlTxt & "<i>"
                itlTagOn = True
            End If
        Else
            If itlTagOn Then
                htmlTxt = htmlTxt & "</i>"
                itlTagOn = False
            End If
        End If

        If .Font.Underline > 0 Then
            If Not ulnTagOn Then
                htmlTxt = htmlTxt & "<u>"
                ulnTagOn = True
            End If
        Else
            If ulnTagOn Then
                htmlTxt = htmlTxt & "</u>"
                ulnTagOn = False
            End If
        End If

        If (Asc(.Text) = 10) Then
            htmlTxt = htmlTxt & "<br>"
        Else
            htmlTxt = htmlTxt & .Text
        End If
    End With
Next

If colTagOn Then
    htmlTxt = htmlTxt & "</font>"
    colTagOn = False
End If
If bldTagOn Then
    htmlTxt = htmlTxt & "</b>"
    bldTagOn = False
End If
If itlTagOn Then
    htmlTxt = htmlTxt & "</i>"
    itlTagOn = False
End If
If ulnTagOn Then
    htmlTxt = htmlTxt & "</u>"
    ulnTagOn = False
End If
htmlTxt = htmlTxt & "</html>"
fnConvert2HTML = htmlTxt
End Function

Function fnGetCol(strCol As String) As String   
Dim rVal, gVal, bVal As String
strCol = Right("000000" & Hex(strCol), 6)
bVal = Left(strCol, 2)
gVal = Mid(strCol, 3, 2)
rVal = Right(strCol, 2)
fnGetCol = rVal & gVal & bVal
End Function

不过,下次试试 google,这样你至少可以说“我已经尝试过 x,y,z 的方式了”。

请注意,这段代码不是我的。

链接:http://social.msdn.microsoft.com/Forums/en-US/626a351a-de17-4389-9ad6-a2be20ce2fd9/convert-contents-of-a-formatted-excel-cell-to-html-format?forum=isvvba

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2016-08-27
    • 1970-01-01
    相关资源
    最近更新 更多