+ Reply to Thread
Results 1 to 3 of 3

VBA to bring Outlook emails to Excel

  1. #1
    Registered User
    Join Date
    08-10-2015
    Location
    Portland
    MS-Off Ver
    2013
    Posts
    3

    VBA to bring Outlook emails to Excel

    Hi there,

    I'm trying to automate copying and pasting the body of a folder of outlook items into one cell per e-mail. I've slapped together this code from other sage users:


    Sub EmailText()
    Dim ObjOutlook As Object
    Dim MyNamespace As Object
    Dim i As Integer
    Dim j As Long
    Dim abody() As String
    Set ObjOutlook = GetObject(, "Outlook.Application")
    Set MyNamespace = ObjOutlook.GetNamespace("MAPI")
    For i = 1 To MyNamespace.GetDefaultFolder(6).Folders("folder").Items.Count
    ThisWorkbook.Sheets(1).Cells(i, 1).Value = MyNamespace.GetDefaultFolder(6).Folders("folder").Items(i).Body
    MyNamespace.GetDefaultFolder(6).Folders("folder").Items(i).Move MyNamespace.GetDefaultFolder(6).Folders("Processed")
    Next
    Set ObjOutlook = Nothing
    Set MyNamespace = Nothing
    End Sub

    This works the way I want and will bring over about n e-mails and then bomb out on the nth e-mail with:

    "Array index out of bounds" on line:

    ThisWorkbook.Sheets(1).Cells(i, 1).Value = MyNamespace.GetDefaultFolder(6).Folders("folder").Items(i).Body

    Any ideas? Why will this run for a handful of e-mails and then stop?

    I feel like the answer is staring me in the face, but I can't see it. Any help is much appreciated!

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA to bring Outlook emails to Excel

    When an email is moved, the other email are reindexed. So say you have 4 emails, if you move email(1), then all the other emails are renumbered from 1 to 3. So by the time you get to index #4 there is no longer an email(4) and it throws an error.

    One solution is to loop backwards...
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-10-2015
    Location
    Portland
    MS-Off Ver
    2013
    Posts
    3

    Re: VBA to bring Outlook emails to Excel

    Thanks! That fixed it.

+ 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. Dump Outlook Emails Into Excel?
    By AstToTheRegionalMGR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2015, 09:28 AM
  2. MS Outlook Emails from Excel using VBA
    By Dbomber in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-06-2013, 02:23 AM
  3. Sending Outlook Emails from/using Excel VBA - Outlook and Excel
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-19-2013, 11:32 AM
  4. [SOLVED] Sending Outlook emails from Excel; Limits to three emails only?
    By BPSJACK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2013, 06:53 AM
  5. Send outlook emails with message in cells to individual emails associated with them
    By abinayan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2011, 06:11 AM
  6. Replies: 0
    Last Post: 01-06-2006, 10:25 AM
  7. [SOLVED] Can you tell Excel to send emails through Outlook?
    By Donald S in forum Excel General
    Replies: 1
    Last Post: 06-30-2005, 01:05 PM

Tags for this Thread

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