簡體   English   中英

使用 VBA 確定 HTML 表格單元格的顏色

[英]Using VBA to determine the colour of an HTML Table cell

我被要求查看是否有任何方法可以根據紅色、琥珀色、綠色狀態自動匯總表格中的多個單元格。 This would be "cake and pie" if it was on an Excel spreadsheet, but I am dealing with an HTML table on an Outlook email and I'm stumped. 使用“innerText”很容易抓取內容,但“bgColor”似乎總是空白,我找不到任何其他可能隱藏數據的屬性......

這是我正在運行的代碼:

公共子 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

結束子

Outlook object model 提供了三種主要的消息體處理方式。

  1. Body屬性返回或設置一個字符串,該字符串表示 Outlook 項目的明文正文。
  2. MailItem class 的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. 字 object model 可用於處理消息體。 有關詳細信息,請參閱第 17 章:使用項目實體

因此,您可以使用 Word object model 來處理表格並更改其 styles。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM