循环浏览文件夹中的项目

时间:2017-10-26 12:43:14

标签: excel vba 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

0 个答案:

没有答案