473,387 Members | 1,374 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,387 software developers and data experts.

VBA Coding to Export Fields from Emails in Outlook to Excel (Help me!!!!!)

I have little to no experience using VBA so I found a sample of code that would allow me to select a folder in Outlook and export that data into Excel. The folder selection box pops up consistently and it opens the file I selected, so that is good. I have tried doing so many things to the code to get it to function properly, but to no avail. One attempt where I removed all of the error handling stuff allowed me to export certain folders, but I still got error messages for some of the other ones. I'm thinking it might have something to do with "Dim msg As Outlook.MailItem". Every time I try to select a folder it says "There are no mail messages to export". I have received error messages: 13, 438, and 50290. The code is posted below. Your help is greatly appreciated.

-Eric

Expand|Select|Wrap|Line Numbers
  1. Sub ExportToExcel()
  2.   On Error GoTo ErrHandler
  3.   Dim appExcel As Excel.Application
  4.   Dim wkb As Excel.Workbook
  5. Dim wks As Excel.Worksheet
  6. Dim rng As Excel.Range
  7. Dim strSheet As String
  8. Dim strPath As String
  9. Dim intRowCounter As Integer
  10. Dim intColumnCounter As Integer
  11. Dim msg As Outlook.MailItem
  12. Dim nms As Outlook.NameSpace
  13. Dim fld As Outlook.MAPIFolder
  14. Dim itm As Object
  15.     strSheet = "Copy of Archive Completed Messages.xlsx"
  16.     strPath = "C:\Users\Documents\"
  17. strSheet = strPath & strSheet
  18. Debug.Print strSheet
  19.  
  20. Set nms = Application.GetNamespace("MAPI")
  21. Set fld = nms.PickFolder
  22.  
  23. If fld Is Nothing Then
  24. MsgBox "There are no mail messages to export", vbOKOnly, "Error"
  25. Exit Sub
  26. ElseIf fld.DefaultItemType <> olMailItem Then
  27. MsgBox "There are no mail messages to export", vbOKOnly, "Error"
  28. Exit Sub
  29. ElseIf fld.Items.Count = 0 Then
  30. MsgBox "There are no mail messages to export", vbOKOnly, "Error"
  31. Exit Sub
  32. End If
  33.  
  34. Set appExcel = CreateObject("Excel.Application")
  35. appExcel.Workbooks.Open (strSheet)
  36. Set wkb = appExcel.ActiveWorkbook
  37. Set wks = wkb.Sheets(1)
  38. wks.Activate
  39. MsgBox "There are no mail messages to export", vbOKOnly, "Error"
  40. appExcel.Application.Visible = True
  41.  
  42.  
  43. For Each itm In fld.Items
  44. intColumnCounter = 1
  45. Set msg = itm
  46. intRowCounter = intRowCounter + 1
  47. Set rng = wks.Cells(intRowCounter, intColumnCounter)
  48. rng.Value = msg.To
  49. intColumnCounter = intColumnCounter + 1
  50. Set rng = wks.Cells(intRowCounter, intColumnCounter)
  51. rng.Value = msg.SenderName
  52. intColumnCounter = intColumnCounter + 1
  53. Set rng = wks.Cells(intRowCounter, intColumnCounter)
  54. rng.Value = msg.LastModificationDate
  55. intColumnCounter = intColumnCounter + 1
  56. Next itm
  57.   Set appExcel = Nothing
  58.   Set wkb = Nothing
  59. Set wks = Nothing
  60. Set rng = Nothing
  61. Set msg = Nothing
  62. Set nms = Nothing
  63. Set fld = Nothing
  64. Set itm = Nothing
  65.   Exit Sub
  66. ErrHandler:  If Err.Number = 1004 Then
  67. MsgBox strSheet & " doesn't exist", vbOKOnly, "Error"
  68. Else
  69. MsgBox Err.Number & "; Description: ", vbOKOnly, "Error"
  70. End If
  71. Set appExcel = Nothing
  72. Set wkb = Nothing
  73. Set wks = Nothing
  74. Set rng = Nothing
  75. Set msg = Nothing
  76. Set nms = Nothing
  77. Set fld = Nothing
  78. Set itm = Nothing
  79. End Sub
Jan 24 '18 #1
3 2130
PhilOfWalton
1,430 Expert 1GB
Eric, I am confused.

This has been posted on the Access Forum, and yet deals with Excel

Your Question is unclear. Do yo want to Export from Outlook and Import into Excel. or Export from Excel and Import into Outlook.

The info I gave you on a previous post just links an Outlook folder to Access so gives the illusion that you are Exporting from Outlook and Importing into Excel.
The 2 queries then put that data into a table.

Excel is not my forte, but I suspect you can do something similar, but I wouldn't be surprised if you get lots of duplicate messages.

Phil
Jan 25 '18 #2
Oops! Sorry about that Phil. Thank you for being the only person to respond to me on these things. So, what my boss wants is the report in access, which you helped me with and works great. However, he wants me to create a code to export Outlook folders directly into Excel so that they can run additional analysis on the emails.

Eric
Jan 25 '18 #3
PhilOfWalton
1,430 Expert 1GB
Why Excel & not Access?

Phil
Jan 25 '18 #4

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

Similar topics

4
by: Paolo | last post by:
Friends, I need help with some code to export different tables to a single spreadsheet in Excel. My excel file is named REPORT and the spreadsheet is named CLIENTS. I do have the code to export...
7
by: Keon | last post by:
Hoi, I'm using a database with alot of records in 1 table (more than 3000). If i want to export this table to excel i only get it till record 2385. Do someone know how i can solve this...
2
by: Siu | last post by:
Hi, I use the following code to export and import a file Excel from resp. into a Web page with the following code: //EXPORT Response.Clear(); Response.Buffer = true; Response.ContentType =...
3
by: JJ_377 | last post by:
I made a user control to gather usa address information and would like to know why the following doesn't work (I am assigning the valid of textbox in a second instance of the control from the first...
0
by: Joseph Burton | last post by:
I'm trying to find the code to export calendar data to excel just like the wizard does in outlook. I have been searching the internet left and right and can't find anything. Can this be done...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
0
by: dino011279 | last post by:
Hello everyone , Could any one please tell me the Vb script to export data from Outlook task onto excel? Regards Dino
0
by: johnlim20088 | last post by:
Hi, Hi someone can help me on this? Currently I have a Listdata.aspx page with datagrid, I wish to export my datagrid content to excel with following code ( with button export click event):- ...
2
budigila
by: budigila | last post by:
Hiya peeps, Okies, I have been trying to work this out for a while now to no avail... I am a beginner to this whole coding thing but have made great strides in my project. Basically what I am...
0
by: =?Utf-8?B?U2hhbQ==?= | last post by:
Hai all, In my application, I need to Export webpage things into hard drive. I am using gridview, and i export all things(with the help of some sites) into excel except images. Please, Can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.