引用 Outlook 消息中的第二个表



>我使用以下代码从电子邮件正文中的表格中提取数据。

我的电子邮件有 3 张桌子。我想在第二个表上获取数据。

如何处理第二个表?

Option Explicit
Sub impOutlookTable()
' point to the desired email
Const strMail As String = "xxx@xxxxx.com"
Dim oApp As Outlook.Application
Dim oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
On Error Resume Next
Set oApp = GetObject(, "OUTLOOK.APPLICATION")
    If (oApp Is Nothing) Then Set oApp = CreateObject("OUTLOOK.APPLICATION")
On Error GoTo 0
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("Inbox").Folders("AMEC-DCC -DS- TRANSMITTAL")
Set oMail = oMapi.Items(oMapi.Items.Count)
' get html table from email object
Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim oElColl As MSHTML.IHTMLElementCollection
With oHTML
    .Body.innerHTML = oMail.HTMLBody
    Set oElColl = .getElementsByTagName("table")
End With
'import in Excel
Dim x As Long, y As Long
For x = 0 To oElColl(0).rows.Length - 1
    For y = 0 To oElColl(0).rows(x).Cells.Length - 1
        Range("A1").Offset(x, y).Value = oElColl(0).rows(x).Cells(y).innerText
    Next y
Next x
Set oApp = Nothing
Set oMapi = Nothing
Set oMail = Nothing
Set oHTML = Nothing
Set oElColl = Nothing
End Sub

关于集合使用的文章

引用

集合时使用 (1) 而不是 (0),oElColl

最新更新