473,411 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Export email sender, subject and other details using vba from outlook to excel sheet

I tried a code and it worked one day. From the next day it's giving my runtime error while retrieving sender name. Code belw:

Expand|Select|Wrap|Line Numbers
  1. Sub ImportEmail()
  2.     ' Add a reference for "Microsoft Outlook nn.n Object Library"
  3.  
  4.     Dim objNS As Namespace
  5.     Dim objFolder As Outlook.MAPIFolder
  6.     Dim objSubFolder As Outlook.MAPIFolder
  7.     Dim objEmail As Outlook.MailItem
  8.     Dim intEmailIndex As Integer
  9.     Dim intRowIndex As Integer
  10.     Dim strMailBoxName As String
  11.     Dim strFolderName  As String
  12.     Dim datFromDate
  13.     Dim datToDate As Date
  14.     Dim objSheet As Worksheet
  15.  
  16.     ' Select folder to process
  17.     Set objNS = Outlook.GetNamespace("MAPI")
  18.     Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
  19.     Set objFolder = objNS.PickFolder
  20.     If TypeName(objFolder) = "Nothing" Then
  21.         MsgBox "No fodler selected, cancelling."
  22.         Exit Sub
  23.     End If
  24.  
  25.     ' Specify from and to date range
  26. '    datFromDate = #1/1/2015#
  27. '    datToDate = #2/1/2016#
  28.     datFromDate = DateValue(InputBox("Enter from date: "))
  29.     datToDate = DateValue(InputBox("Enter to date: "))
  30.  
  31.     ' Export into first sheet in Excel
  32.     Set objSheet = ThisWorkbook.Sheets(1)
  33.     objSheet.Activate
  34.     objFolder.Items.Sort "Received"
  35.  
  36.     ' Add Column Headers
  37.     objSheet.Cells(1, 1) = "Sender Name"
  38.     objSheet.Cells(1, 2) = "Sender Email"
  39.     objSheet.Cells(1, 3) = "To"
  40.     objSheet.Cells(1, 4) = "Subject"
  41.     objSheet.Cells(1, 5) = "Received Time"
  42.     objSheet.Cells(1, 6) = "Folder Name"
  43.     objSheet.Cells(1, 7) = "Body"
  44.  
  45.     ' Process each email item in the selected folder
  46.     intRowIndex = 1
  47.     For intEmailIndex = 1 To objFolder.Items.Count
  48.         Set objEmail = objFolder.Items.Item(intEmailIndex)
  49.         ' Only process mail in the date range we want
  50.         If objEmail.ReceivedTime >= datFromDate And objEmail.ReceivedTime <= datToDate Then
  51.            intRowIndex = intRowIndex + 1
  52.            objSheet.Cells(intRowIndex, 1).Select
  53.            objSheet.Cells(intRowIndex, 1) = objEmail.SenderName
  54.            objSheet.Cells(intRowIndex, 2) = objEmail.SenderEmailAddress
  55.            objSheet.Cells(intRowIndex, 3) = objEmail.To
  56.            objSheet.Cells(intRowIndex, 4) = objEmail.Subject
  57.            objSheet.Cells(intRowIndex, 5) = objEmail.ReceivedTime
  58.            objSheet.Cells(intRowIndex, 6) = objFolder.Name
  59.            objSheet.Cells(intRowIndex, 7) = objEmail.Body
  60.         End If
  61.     Next intEmailIndex
  62.  
  63.     MsgBox (intRowIndex - 1) & " emails selected and exported."
  64.  
  65. End Sub
Dec 31 '18 #1
6 4241
Luuk
1,047 Expert 1GB
Info on the exact error is missing...

Like this (sorry for the fact that i have a Dutch version):



Most import thing is what line is shown when you click 'Foutopsporing' (the currently selected button)

When you did this yourself, you would have found that this line is causing the problem:

Attached Images
File Type: png error.png (4.9 KB, 1553 views)
File Type: png received.png (3.5 KB, 1418 views)
Jan 1 '19 #2
I am getting an error at objEmail.SenderName and the error is Run-time error '287':
Application-defined or object-defined error. The same is related for Sender email address, objEmail.To and objEmail.Body as well.
Hope the details will help to understand the issue. Please help
Jan 1 '19 #3
Luuk
1,047 Expert 1GB
I am not able to reproduce this error.

Can you try to add this line of code on line#3
Expand|Select|Wrap|Line Numbers
  1.     Application.EnableEvents = True
  2.  
and see if that changes things...
Jan 1 '19 #4
I tried with enable events but still getting the same error. One point I would like to inform. It worked for a day and there after not working. I am using this code on an office outlook. Is that something we need to manage the security permissions to get sender name? Except sender name, sender email and body fiepds, remaining fields like subject, received time etc are getting exported to excel.
Jan 2 '19 #5
Luuk
1,047 Expert 1GB
I am sorry to say that I could not find a good reference which explains 'Run-time error '287'' .

The 'best' solution seems to be:
source: https://social.msdn.microsoft.com/Fo...orum=accessdev
Expand|Select|Wrap|Line Numbers
  1. Function YourFunction()
  2.   On Error Goto Error_Proc
  3.  
  4.   ' all your usual code goes here'
  5.  
  6.  
  7. Exit_Proc:
  8.   'this is your exit portion'
  9.   'all exits will be directed here'
  10.   'use this to clean up any open objects'
  11.   Exit Function
  12. Error_Proc:
  13.   'this is your error handler'
  14.   'with the On Error statement at the top,'
  15.   'any errors jump to the specified label'
  16.   ''
  17.   'check errors:'
  18.   Select Case Err.Number
  19.     Case 287:
  20.       Resume Exit_Proc 'ignore the error'
  21.     Case Else:
  22.       MsgBox "Error encountered: " & Err.Description
  23.       Resume Exit_Proc 'display a message then exit'
  24. End Function
I cannot test that because I do not (yet) see that error.
The code should just ignore the error, and tries to continue.
Jan 2 '19 #6
The code worked for one day and from the n3xt day it's not working. Looks some security permissions have been disabled unknowingly while testing the code. Can anyone help me to check where the security settings exists in outlook and can those be handled through macro while running the code and revert back to the existing settings.
Jan 2 '19 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: sifar | last post by:
Hi, I am right now learning PHP & want to know if there is a way to send web form data to an Excel sheet located on a network. My windows xp PC doesnot have a copy of Excel. Also i am not...
2
by: TM | last post by:
I have an Excel sheet where I setup my needed formatting, page settings, ect, and would like to take my data from a datagrid and paste it into the excel file and print the excel file. Any idea...
5
by: Siv | last post by:
Hi, A little while ago I asked if anyone could help me with how to create an email using MS Outlook that contained an embedded picture file. Thanks to Jay Harlow I was able to get this working...
5
by: handokowidjaja | last post by:
Hi All, I'm trying to automate sending an email with an attachment in our environment (access 97) using Outlook Express ( we dont have MS outlook or other fancy stuff). Does anybody knows how to...
2
by: arxguru | last post by:
Hi, I am having some of 1,00,000 lines of records in txt file and i need to put it in to excel sheet but in excel per sheet we can occupy only 65536 lines and i like to do it automatically and...
1
by: hakeemkazmi | last post by:
hi there,i am able to export the gridview data to excel sheet successfully but my gridview has 3 buttonfields like details etc etc which when clicked give the details,but i dont want those...
7
by: Paridevi | last post by:
Hai , i want to send email in .Net Using OutLook Express,My Project is Web Application using vb.Net 2003 with SQL Server 2000.I have searched a lot in Google, but ican't get any...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
2
by: satwinder singh | last post by:
Please help me regarding how to Export the Data from DataSet or GridView into Excel sheet. Kind Regards, Satwinder singh
0
by: Dila Ram Gurung | last post by:
I want to write a code in .NET C# which will fetch data from actively running excel sheet. To be more elaborative : I am using an excel sheet which automatically updates its cell value through...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.