+ Reply to Thread
Results 1 to 5 of 5

Download Outlook mail to excel - runtime error, array index out of bounds

  1. #1
    Registered User
    Join Date
    02-21-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    2

    Download Outlook mail to excel - runtime error, array index out of bounds

    Hi,

    Any help would be greatly appreciated with the code below. I'm getting the error on line 'If Folder.Items.Item(iRow).Subject = SubjectLine Then'

    Thanks!


    Option Explicit
    Sub Download_Outlook_Mail_To_Excel()
    'Add Tools->References->"Microsoft Outlook nn.n Object Library"
    'nn.n varies as per our Outlook Installation
    Dim Folder As Outlook.MAPIFolder
    Dim sFolders As Outlook.MAPIFolder
    Dim iRow As Integer, oRow As Integer
    Dim MailBoxName As String, Pst_Folder_Name As String
    Dim SubjectLine As String
    Dim recip As Recipient
    Dim SavePath As String
    Dim RangeLower As Integer, RangeUpper As Integer, InputPath As String


    'Mailbox or PST Main Folder Name (As how it is displayed in your Outlook Session)
    MailBoxName = ThisWorkbook.Sheets(1).Range("Mailbox")
    SubjectLine = ThisWorkbook.Sheets(1).Range("Subject")
    RangeLower = ThisWorkbook.Sheets(1).Range("Range_Lower")
    RangeUpper = ThisWorkbook.Sheets(1).Range("Range_Upper")
    InputPath = ThisWorkbook.Sheets(1).Range("Input_Path")

    'Mailbox Folder or PST Folder Name (As how it is displayed in your Outlook Session)
    Pst_Folder_Name = ThisWorkbook.Sheets(1).Range("Folder") 'Sample "Inbox" or "Sent Items"

    'To directly a Folder at a high level
    'Set Folder = Outlook.Session.Folders(MailBoxName).Folders(Pst_Folder_Name)

    'To access a main folder or a subfolder (level-1)
    For Each Folder In Outlook.Session.Folders(MailBoxName).Folders
    If VBA.UCase(Folder.Name) = VBA.UCase(Pst_Folder_Name) Then GoTo Label_Folder_Found
    For Each sFolders In Folder.Folders
    If VBA.UCase(sFolders.Name) = VBA.UCase(Pst_Folder_Name) Then
    Set Folder = sFolders
    GoTo Label_Folder_Found
    End If
    Next sFolders
    Next Folder

    Label_Folder_Found:
    If Folder.Name = "" Then
    MsgBox "Invalid Data in Input"
    GoTo End_Lbl1:
    End If

    'Read Through each Mail and export the details to Excel for Email Archival
    ThisWorkbook.Sheets(2).Activate
    Folder.Items.Sort "Received"

    'Insert Column Headers
    ThisWorkbook.Sheets(2).Cells(1, 1) = "Sender"
    ThisWorkbook.Sheets(2).Cells(1, 2) = "Subject"
    ThisWorkbook.Sheets(2).Cells(1, 3) = "Date"
    ThisWorkbook.Sheets(2).Cells(1, 4) = "EmailID"
    ThisWorkbook.Sheets(2).Cells(1, 5) = "Email Address"

    'Export eMail Data from PST Folder
    oRow = 1
    For iRow = RangeLower To RangeUpper 'Folder.Items.Count
    'If condition to import mails received in last 60 days
    'To import all emails, comment or remove this IF condition
    'If VBA.DateValue(VBA.Now) - VBA.DateValue(Folder.Items.Item(iRow).ReceivedTime) <= 5
    If Folder.Items.Item(iRow).Subject = SubjectLine Then
    SavePath = InputPath
    For Each recip In Folder.Items.Item(iRow).Recipients
    oRow = oRow + 1
    ThisWorkbook.Sheets(2).Cells(oRow, 1).Select
    ThisWorkbook.Sheets(2).Cells(oRow, 1) = Folder.Items.Item(iRow).SenderName
    ThisWorkbook.Sheets(2).Cells(oRow, 2) = Folder.Items.Item(iRow).Subject
    ThisWorkbook.Sheets(2).Cells(oRow, 3) = Folder.Items.Item(iRow).ReceivedTime
    ThisWorkbook.Sheets(2).Cells(oRow, 4) = recip.Name 'Folder.Items.Item(iRow).Recipients
    ThisWorkbook.Sheets(2).Cells(oRow, 5) = recip.Address
    'ThisWorkbook.Sheets(1).Cells(oRow, 6) = Folder.Items.Item(iRow).Body
    Next
    SavePath = SavePath & SubjectLine & iRow & ".msg"
    Folder.Items.Item(iRow).SaveAs SavePath
    End If
    Next iRow
    MsgBox "Outlook Mails Extracted to Excel"
    Set Folder = Nothing
    Set sFolders = Nothing

    End_Lbl1:
    End Sub

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Download Outlook mail to excel - runtime error, array index out of bounds

    Your arrays are different in size
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    02-21-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    2

    Re: Download Outlook mail to excel - runtime error, array index out of bounds

    Thanks Keebellah. A friend provided this code and It has been working up until now. I'm unable to figure out what has changed. Any further help would be appreciated.

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Download Outlook mail to excel - runtime error, array index out of bounds

    I suggest you ask this friend. I do not have the structure you require for this code to run so, sorry no go. Guessing is not my game.
    You are asking and using your pst folder structure, this is unique for your MAPi structure so, can't help
    My first error is with the first MailBox=

    The error you get happens way down, so what you should do is step through the code line by line and check the values, troubleshooting requires patience and perseverance.
    My guess is that the reference to either iRow or .Subject or Subjectline is incorrect.
    Sorry, but it has to be on your system.

    If it was working until now, track back, what changed?

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Download Outlook mail to excel - runtime error, array index out of bounds

    BTW when you post code post it using the code indicators and also indent your code it makes it readable

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calling Webservice: Error (Index was outside the bounds of the array)
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-08-2015, 12:45 AM
  2. excel/outlook runtime error 440
    By Spotniq in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2015, 02:57 AM
  3. E-Mail Using Excel VBA & Outlook Error
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-29-2014, 01:54 AM
  4. [SOLVED] "The index into the specified collection is out of bounds" error
    By kundterra in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-05-2014, 12:32 PM
  5. Runtime error 381: Could not set the list property.Invalid property array index
    By rohith4prithvi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-23-2014, 10:24 PM
  6. Excel macro to get outlook contact info (runtime error/crashing)
    By ZTHawk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-19-2012, 04:19 AM
  7. [SOLVED] Calling Outlook in Excel runtime error
    By Nicole Seibert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-15-2006, 04:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1