【发布时间】:2018-07-18 03:37:39
【问题描述】:
我有以下代码用于从 Outlook 电子邮件中提取表格。
它只查看最新的电子邮件。我需要在电子邮件的主题中匹配一个字符串来确定要从哪封电子邮件中提取。
我需要在代码中添加什么?
Option Explicit
Sub impOutlookTable()
Dim wkb As Workbook
Set wkb = Workbooks.Add
Sheets("Sheet1").Cells.ClearContents
' point to the desired email
Const strMail As String = "first.last@outlook.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")
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
wkb.SaveAs "C:\Users\user\.spyder-py3\Outlook\tables.xlsx"
End Sub
【问题讨论】: