【发布时间】:2021-04-06 10:35:23
【问题描述】:
我正在尝试将 HTML 表格从电子邮件导入 Excel。
我在这里偶然发现了可以从选定文件夹导入所有 html 表的代码。我想添加一个选项来选择指定的日期。
已添加以下行
If OutlookMail.ReceivedTime >= Range("Email_ReciptDate").Value Then)
我收到一个错误。
Dim oApp As Outlook.Application
Dim oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
Dim HTMLdoc As MSHTML.HTMLDocument
Dim tables As MSHTML.IHTMLElementCollection
Dim table As MSHTML.HTMLTable
Dim x As Long, y As Long
Dim destCell As Range
With ActiveSheet
Set destCell = .Cells(Rows.Count, "A").End(xlUp)
End With
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").PickFolder
If Not oMapi Is Nothing Then
For Each oMail In oMapi.Items
If OutlookMail.ReceivedTime >= Range("Email_ReciptDate").Value Then
'Get HTML tables from email object
Set HTMLdoc = New MSHTML.HTMLDocument
With HTMLdoc
.Body.innerHTML = oMail.HTMLBody
Set tables = .getElementsByTagName("table")
End With
'Import each table into Excel
For Each table In tables
For x = 0 To table.Rows.Length - 1
For y = 0 To table.Rows(x).Cells.Length - 1
destCell.Offset(x, y).Value = table.Rows(x).Cells(y).innerText
Next y
Next x
Set destCell = destCell.Offset(x)
Next
End If
Next
MsgBox "Finished"
End If
Set oApp = Nothing
Set oMapi = Nothing
Set oMail = Nothing
Set HTMLdoc = Nothing
Set tables = Nothing
【问题讨论】:
-
请更清楚一点,您想要实现什么样的修改。
-
基本上我想要实现的是能够将 2 封电子邮件的 HTML 正文合并到一封电子邮件中。然后发送出去。但是,这似乎只是将 HTML 内部文本提取到 excel 中。有没有办法我可以简单地从两封电子邮件中获取 HTML 表格并将它们组合成 1(保留原始表格格式)