+ Reply to Thread
Results 1 to 11 of 11

Pulling info from Outlook using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2023
    Location
    USA
    MS-Off Ver
    2016
    Posts
    6

    Pulling info from Outlook using VBA

    I found the below script on another site, but it does not work for certain MailItems (such as SenderName, SenderEmailAddress, and Body). However, ReceivedTime and Subject work fine.

    With "SenderName" and "SenderEmailAddress" I get this error:

    Run-time error '?2147467259 (80004005)':
    Method 'SenderName' of object '_Mailltem' failed

    With Body I get this error:

    Run-time error '287':
    Application-defined or object-defined error

    I am brand new to using visual basic so any help would be appreciated. Thanks.


    Sub ImportEMail()
    Dim olA As Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olF As Outlook.MAPIFolder
    Dim olM As Outlook.MailItem
    
    Dim iRow As Integer
    
    Set olA = New Outlook.Application
    Set olNS = olA.GetNamespace("MAPI")
    
    Set olF = olNS.PickFolder
    
    iRow = 1
    For Each olM In olF.Items
    With ActiveSheet
    .Cells(iRow, 1) = olM.SenderName
    .Cells(iRow, 1) = olM.SenderEmailAddress
    .Cells(iRow, 2) = olM.ReceivedTime
    .Cells(iRow, 3) = olM.Subject
    .Cells(iRow, 4) = olM.Body
    
    iRow = iRow + 1
    End With
    Next
    
    Set olM = Nothing
    Set olF = Nothing
    Set olNS = Nothing
    Set olA = Nothing
    
    End Sub
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Our guidelines recommend code tags. I have added them for you this time because you are a new member. Also consider indentation to show structure. --6StringJazzer
    Last edited by 6StringJazzer; 08-18-2023 at 12:36 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,226

    Re: Pulling info from Outlook using VBA

    Welcome to the Forum Excel3257705!

    You said it does not work for "certain" email items. What can you tell us about the particular emails that it does not work for? I will see if I can run this code and see what's happening.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    08-17-2023
    Location
    USA
    MS-Off Ver
    2016
    Posts
    6

    Re: Pulling info from Outlook using VBA

    Thanks for your reply. My reference to "certain" MailItems was intended to mean properties of Outlook not particular e-mails. For instance if the below code is used, it works fine. Also, I am choosing a folder which only has e-mails in it (it doesn't have any meeting notices, etc.)

    Sub ImportEMail()
    Dim olA As Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olF As Outlook.MAPIFolder
    Dim olM As Outlook.MailItem
    
    Dim iRow As Integer
    
    Set olA = New Outlook.Application
    Set olNS = olA.GetNamespace("MAPI")
    
    Set olF = olNS.PickFolder
    
    iRow = 1
    For Each olM In olF.Items
    With ActiveSheet
    '.Cells(iRow, 1) = olM.SenderName
    '.Cells(iRow, 1) = olM.SenderEmailAddress
    .Cells(iRow, 2) = olM.ReceivedTime
    .Cells(iRow, 3) = olM.Subject
    '.Cells(iRow, 4) = olM.Body
    
    iRow = iRow + 1
    End With
    Next
    
    Set olM = Nothing
    Set olF = Nothing
    Set olNS = Nothing
    Set olA = Nothing
    
    End Sub
    But if the lines with "SenderName, SenderEmailAddress, and Body" are added back in, I get the errors referenced in my previous post.

    Also, the below code doesn't work either and contains a specific reference to mail items (the line with "If objItem.Class = olMail"), so I don't think the class of the item is the issue.



    Option Explicit
    
    Private Sub CommandButton1_Click()
        On Error GoTo ErrHandler
        
        ' Set Outlook application object.
        Dim objOutlook As Object
        Set objOutlook = CreateObject("Outlook.Application")
        
        Dim objNSpace As Object     ' Create and Set a NameSpace OBJECT.
        ' The GetNameSpace() method will represent a specified Namespace.
        Set objNSpace = objOutlook.GetNamespace("MAPI")
        
        Dim myFolder As Object  ' Create a folder object.
        Set myFolder = objNSpace.GetDefaultFolder(olFolderInbox)
        
        Dim objItem As Object
        Dim iRows, iCols As Integer
        iRows = 2
    
        ' Loop through each item in the folder.
        For Each objItem In myFolder.Items
            If objItem.Class = olMail Then
            
                Dim objMail As Outlook.MailItem
                Set objMail = objItem
    
                Cells(iRows, 1) = objMail.SenderEmailAddress
                Cells(iRows, 2) = objMail.To
                Cells(iRows, 3) = objMail.Subject
                Cells(iRows, 4) = objMail.ReceivedTime
            End If
            iRows = iRows + 1
        Next
        Set objMail = Nothing
       
        ' Release.
        Set objOutlook = Nothing
        Set objNSpace = Nothing
        Set myFolder = Nothing
    ErrHandler:
        Debug.Print Err.Description
    End Sub
    Thanks again for your assistance.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,226

    Re: Pulling info from Outlook using VBA

    I am getting a "type mismatch" runtime error when I encounter a meeting notice, because it is in the Items collection but is not a MailItem object. But that is not the same error you are getting.

    I will look into how to resolve this but it may not fix your problem.

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Pulling info from Outlook using VBA

    Hi, try this

    Sub jec()
     Dim ar, it, x As Long
     With CreateObject("outlook.application").getnamespace("mapi").pickfolder
       ReDim ar(.items.Count, 4)
       For Each it In .items
         If TypeName(it) = "MailItem" Then
            ar(x, 0) = it.SenderName
            ar(x, 1) = it.SenderEmailAddress
            ar(x, 2) = it.ReceivedTime
            ar(x, 3) = it.Subject
            ar(x, 4) = it.Body
            x = x + 1
         End If
       Next
       Cells(1, 1).Resize(x, 5) = ar
     End With
    End Sub

  6. #6
    Registered User
    Join Date
    08-17-2023
    Location
    USA
    MS-Off Ver
    2016
    Posts
    6

    Re: Pulling info from Outlook using VBA

    Quote Originally Posted by JEC. View Post
    Hi, try this

    Sub jec()
     Dim ar, it, x As Long
     With CreateObject("outlook.application").getnamespace("mapi").pickfolder
       ReDim ar(.items.Count, 4)
       For Each it In .items
         If TypeName(it) = "MailItem" Then
            ar(x, 0) = it.SenderName
            ar(x, 1) = it.SenderEmailAddress
            ar(x, 2) = it.ReceivedTime
            ar(x, 3) = it.Subject
            ar(x, 4) = it.Body
            x = x + 1
         End If
       Next
       Cells(1, 1).Resize(x, 5) = ar
     End With
    End Sub
    Thanks for your reply. I get the same errors as before with your code. Also like before, if I take out the line with SenderName, SenderEmailAddress, and Body, then it works fine.

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Pulling info from Outlook using VBA

    What if you only comment out the body? Is it giving the error with the first item already?
    Last edited by JEC.; 08-18-2023 at 01:33 PM.

  8. #8
    Registered User
    Join Date
    08-17-2023
    Location
    USA
    MS-Off Ver
    2016
    Posts
    6

    Re: Pulling info from Outlook using VBA

    Quote Originally Posted by JEC. View Post
    What if you only comment out the body? Is it giving the error with the first item already?
    Same error:

    Run-time error '-2147467259 (80004005)':
    Method 'SenderName' of object '_Mailltem' failed

    I found a post on another forum re: a similar issue which contained the following:

    "I had a similar problem, and it bugged the heck out of me. My eventual fix (workaround) was using a variable, which you wouldn't think would matter. Declare a string variable and then populate and use it."

    Unfortunately, I don't know enough to use the fix described above.

    Thanks.

  9. #9
    Registered User
    Join Date
    08-17-2023
    Location
    USA
    MS-Off Ver
    2016
    Posts
    6

    Re: Pulling info from Outlook using VBA

    Quote Originally Posted by Excel3257705 View Post
    Same error:

    Run-time error '-2147467259 (80004005)':
    Method 'SenderName' of object '_Mailltem' failed

    I found a post on another forum re: a similar issue which contained the following:

    "I had a similar problem, and it bugged the heck out of me. My eventual fix (workaround) was using a variable, which you wouldn't think would matter. Declare a string variable and then populate and use it."

    Unfortunately, I don't know enough to use the fix described above.

    Thanks.
    The post described above stating using string variables was the solution in a similar instance is #57490 from accessforums dot net

  10. #10
    Registered User
    Join Date
    08-17-2023
    Location
    USA
    MS-Off Ver
    2016
    Posts
    6

    Re: Pulling info from Outlook using VBA

    This morning I posted this same question to the Experts Exchange website. If I receive an answer I will post it here. Thanks.

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,226

    Re: Pulling info from Outlook using VBA

    Please post a link to your thread on Experts Exchange. It's fine to post elsewhere but our rules require a link when you do.

+ 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. [SOLVED] Pulling info from one sheet to another
    By melymelmela in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-17-2019, 10:47 PM
  2. VBA for pulling out info on changes
    By areskoug in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-10-2017, 03:33 AM
  3. [SOLVED] Pulling info from other sheets
    By Judylily in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-20-2017, 04:27 PM
  4. Need help pulling info from a list
    By andyaf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2013, 05:05 PM
  5. Pulling info from one sheet to another
    By floridavera in forum Excel General
    Replies: 6
    Last Post: 05-20-2011, 08:46 AM
  6. Pulling info from worksheet
    By jcarstens in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-20-2009, 02:41 PM
  7. Pulling info into another sheet
    By Teric506 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-26-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