使用VBA将Outlook表格转换为Excel

我试图导出Outlook的Outlook消息到Excel。

所有邮件都在同一个邮箱中,具有相同的主题,可以包含多个具有不同行数的表。 我设法得到第一封邮件,但它不会遍历整个邮箱。

提前致谢!

Option Explicit Sub impToExcel() ' point to the desired email Const strMail As String = "jonfo@company.com" Dim oApp As Outlook.Application Dim Nsp As Namespace Dim Fldr As MAPIFolder Dim oMail As Outlook.MailItem Dim Var As Variant Set oApp = New Outlook.Application Set Nsp = oApp.GetNamespace("MAPI") Set Fldr = Nsp.GetDefaultFolder(olFolderInbox).Folders("Svar") Set oMail = Fldr.Items(Fldr.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 Each Var In Fldr.Items 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 Next Var Set oApp = Nothing Set Fldr = Nothing Set oMail = Nothing Set oHTML = Nothing Set oElColl = Nothing End Sub