【问题标题】:How to determine the colour of an HTML Table cell?使用 VBA 确定 HTML 表格单元格的颜色
【发布时间】:2020-11-04 15:58:25
【问题描述】:

有人问我是否有任何方法可以根据红色、琥珀色、绿色状态自动汇总表格中的多个单元格。如果它在 Excel 电子表格上,这将是“蛋糕和馅饼”,但我正在处理 Outlook 电子邮件上的 HTML 表格,我被难住了。使用“innerText”很容易抓取内容,但“bgColor”似乎总是空白,我找不到任何其他可能隐藏数据的属性...

这是我正在运行的代码:

Public Sub TableScrubber()

'此宏询问 Outlook 电子邮件的正文以查找表格,然后显示每个单元格的内容

Dim outlookHTML As MSHTML.HTMLDocument: Set outlookHTML = New MSHTML.HTMLDocument
Dim elementCollection As MSHTML.IHTMLElementCollection

Dim iItem As Single
Dim iTable As Single
Dim iRow As Long
Dim iColumn As Long
Dim activeSelection As Outlook.Selection
Dim selectedObject As Object
Dim selectedMailItem As mailItem

Dim itemInfo As String

Set activeSelection = Application.ActiveExplorer.Selection

If activeSelection.Count > 0 Then

    For iItem = 1 To activeSelection.Count
        Set selectedObject = activeSelection.Item(iItem)
        If (TypeOf selectedObject Is Outlook.mailItem) Then
            Set selectedMailItem = selectedObject
            itemInfo = "Message Subject: " & selectedMailItem.Subject
            
            'save Outlook email's html body (tables)
            With outlookHTML
                .Body.innerHTML = selectedMailItem.HTMLBody
                Set elementCollection = .getElementsByTagName("table")
            End With
            
            For iTable = 0 To elementCollection.Length - 1
                For iRow = 0 To elementCollection(iTable).Rows.Length - 1
                    For iColumn = 0 To elementCollection(iTable).Rows(iRow).Cells.Length - 1
                        itemInfo = "The text in this cell is: " & elementCollection(iTable).Rows(iRow).Cells(iColumn).innerText
                        
                        itemInfo = "The color of this cell is: " & elementCollection(iTable).Rows(iRow).Cells(iColumn).bgColor
                    Next iColumn
                Next iRow
            Next iTable
        End If
    Next iItem
End If
Set outlookHTML = Nothing
Set elementCollection = Nothing

结束子

【问题讨论】:

  • 当您处理 html 时,样式可能完全在 CSS 样式和/或类中完成。在这种情况下,请查看 developer.mozilla.org/en-US/docs/Web/API/Window/… 的计算样式。然后您可以检索单个单元格/行/表格的样式背景颜色
  • 您好 JMP -- 我已经添加了 VBA 代码,但不知道如何在没有编辑器解释它并将其转换为纯文本的情况下添加 HTML...

标签: html vba outlook background-color


【解决方案1】:

Outlook 对象模型提供了三种处理邮件正文的主要方法。

  1. Body 属性返回或设置一个字符串,该字符串表示 Outlook 项目的明文正文。
  2. MailItem 类的HTMLBody 属性返回或设置表示指定项目的HTML 正文的字符串。设置 HTMLBody 属性将始终立即更新 Body 属性。例如:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. Word 对象模型可用于处理消息正文。请参阅Chapter 17: Working with Item Bodies 了解更多信息。

因此,您可以使用 Word 对象模型来处理表格并更改其样式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-17
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多