【发布时间】: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