Hello !!!
I found this Sub here in MrExcel....
Sub OLook_to_Excel()
Dim oApp As Outlook.Application, oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim oElColl As MSHTML.IHTMLElementCollection, x&, y&
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(1).Folders(1) ' desired folder
Set oMail = oMapi.Items(oMapi.Items.Count) ' desired email
With oHTML
.Body.innerHTML = oMail.htmlBody
Set oElColl = .getElementsByTagName("table")
End With
For x = 0 To oElColl(0).Rows.Length - 1 ' import
For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
[A1].Offset(x, y) = oElColl(0).Rows(x).Cells.innerText
Next
Next
Set oApp = Nothing: Set oMapi = Nothing
Set oMail = Nothing: Set oHTML = Nothing
Set oElColl = Nothing
End Sub
Trying to use it.... I am getting error "Array index is out of bounds" when execution reaches:
Set oMail = oMapi.Items(oMapi.Items.Count) ' desired email
The value of oMail.Items.Count is zero. I do have unread emails in my inbox. In fact the email I am trying to process is the latest received and unread.
Could you please provide your valuable advise?
Thank you very much
e1375
I found this Sub here in MrExcel....
Sub OLook_to_Excel()
Dim oApp As Outlook.Application, oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim oElColl As MSHTML.IHTMLElementCollection, x&, y&
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(1).Folders(1) ' desired folder
Set oMail = oMapi.Items(oMapi.Items.Count) ' desired email
With oHTML
.Body.innerHTML = oMail.htmlBody
Set oElColl = .getElementsByTagName("table")
End With
For x = 0 To oElColl(0).Rows.Length - 1 ' import
For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
[A1].Offset(x, y) = oElColl(0).Rows(x).Cells.innerText
Next
Next
Set oApp = Nothing: Set oMapi = Nothing
Set oMail = Nothing: Set oHTML = Nothing
Set oElColl = Nothing
End Sub
Trying to use it.... I am getting error "Array index is out of bounds" when execution reaches:
Set oMail = oMapi.Items(oMapi.Items.Count) ' desired email
The value of oMail.Items.Count is zero. I do have unread emails in my inbox. In fact the email I am trying to process is the latest received and unread.
Could you please provide your valuable advise?
Thank you very much
e1375