使用Excel vba将表从Outlook电子邮件导入Excel

时间:2018-09-03 14:28:09

标签: excel excel-vba outlook export

我试图从Outlook文件夹中的电子邮件中将表格导入Excel工作簿中,但是代码不起作用。

Option Explicit
Sub impOutlookTable()

Const strMail As String = "me@yahoo.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("parent").Folders("subfolder")
Set oMail = oMapi.Items(oMapi.Items.Count)

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

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

我有这样的问题,我一直在此行中出现错误“对象变量或未设置块变量”:

For x = 0 To oElColl(0).Rows.Length - 1

在调试时,x显示为0,oHTML,oElColl显示为“ [object]”

0 个答案:

没有答案