{"id":115,"date":"2015-09-10T10:36:29","date_gmt":"2015-09-10T10:36:29","guid":{"rendered":"http:\/\/99excel.com\/?p=115"},"modified":"2021-05-14T16:09:21","modified_gmt":"2021-05-14T16:09:21","slug":"vba-code-for-export-all-email-address-and-mail-message-from-outlook-to-excel","status":"publish","type":"post","link":"https:\/\/99excel.com\/home\/vba-code-for-export-all-email-address-and-mail-message-from-outlook-to-excel\/","title":{"rendered":"VBA code for export all email address and mail message from outlook to excel"},"content":{"rendered":"<p>Sub ExportToExcel()<br \/>On Error GoTo ErrHandler<br \/>Set appExcel = CreateObject(&#8220;Excel.Application&#8221;)<br \/>Set appExcel = Excel.Application<br \/>Dim wkb As Excel.Workbook<br \/>Dim wks As Excel.Worksheet<br \/>Dim rng As Excel.Range<br \/>Dim strSheet As String<br \/>Dim strPath As String<br \/>Dim intRowCounter As Integer<br \/>Dim intColumnCounter As Integer<br \/>Dim msg As Outlook.MailItem<br \/>Dim nms As Outlook.NameSpace<br \/>Dim fld As Outlook.MAPIFolder<br \/>Dim itm As Object<br \/>strSheet = &#8220;OutlookItems.xls&#8221;<br \/>strPath = &#8220;C:Attendance&#8221;<br \/>strSheet = strPath &amp; strSheet<\/p>\n<p>Debug.Print strSheet<br \/> &#8216;Select export folder<br \/>Set nms = Application.GetNamespace(&#8220;MAPI&#8221;)<br \/>Set fld = nms.PickFolder<br \/> &#8216;Handle potential errors with Select Folder dialog box.<br \/>If fld Is Nothing Then<br \/>MsgBox &#8220;There are no mail messages to export&#8221;, vbOKOnly, &#8220;Error&#8221;<br \/>Exit Sub<br \/>ElseIf fld.DefaultItemType &lt;&gt; olMailItem Then<br \/>MsgBox &#8220;There are no mail messages to export&#8221;, vbOKOnly, &#8220;Error&#8221;<br \/>Exit Sub<br \/>ElseIf fld.Items.Count = 0 Then<br \/>MsgBox &#8220;There are no mail messages to export&#8221;, vbOKOnly, &#8220;Error&#8221;<br \/>Exit Sub<br \/>End If<br \/> &#8216;Open and activate Excel workbook.<br \/>Set appExcel = CreateObject(&#8220;Excel.Application&#8221;)<br \/>appExcel.Workbooks.Open (strSheet)<br \/>Set wkb = appExcel.ActiveWorkbook<br \/>Set wks = wkb.Sheets(1)<br \/>wks.Activate<br \/>appExcel.Application.Visible = True<br \/> &#8216;Copy field items in mail folder.<br \/>For Each itm In fld.Items<\/p>\n<p>intColumnCounter = 1<\/p>\n<p>Set msg = itm<br \/>intRowCounter = intRowCounter + 1<br \/>Set rng = wks.Cells(intRowCounter, intColumnCounter)<br \/>rng.Value = msg.To<br \/>intColumnCounter = intColumnCounter + 1<br \/>Set rng = wks.Cells(intRowCounter, intColumnCounter)<br \/>rng.Value = msg.SenderEmailAddress<br \/>intColumnCounter = intColumnCounter + 1<br \/>Set rng = wks.Cells(intRowCounter, intColumnCounter)<br \/>rng.Value = msg.Subject<br \/>intColumnCounter = intColumnCounter + 1<br \/>Set rng = wks.Cells(intRowCounter, intColumnCounter)<br \/>rng.Value = msg.SentOn<br \/>intColumnCounter = intColumnCounter + 1<br \/>Set rng = wks.Cells(intRowCounter, intColumnCounter)<br \/>rng.Value = msg.ReceivedTime<\/p>\n<p>Next itm<\/p>\n<p>Set appExcel = Nothing<br \/>Set wkb = Nothing<br \/>Set wks = Nothing<br \/>Set rng = Nothing<br \/>Set msg = Nothing<br \/>Set nms = Nothing<br \/>Set fld = Nothing<br \/>Set itm = Nothing<br \/>Exit Sub<br \/>ErrHandler: If Err.Number = 1004 Then<br \/>MsgBox strSheet &amp; &#8221; doesn&#8217;t exist&#8221;, vbOKOnly, &#8220;Error&#8221;<br \/>Else<br \/>MsgBox Err.Number &amp; &#8220;; Description: &#8220;, vbOKOnly, &#8220;Error&#8221;<\/p>\n<p>End If<br \/>Set appExcel = Nothing<br \/>Set wkb = Nothing<br \/>Set wks = Nothing<br \/>Set rng = Nothing<br \/>Set msg = Nothing<br \/>Set nms = Nothing<br \/>Set fld = Nothing<br \/>Set itm = Nothing<br \/>End Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sub ExportToExcel()On Error GoTo ErrHandlerSet appExcel = CreateObject(&#8220;Excel.Application&#8221;)Set appExcel = Excel.ApplicationDim wkb As Excel.WorkbookDim wks As Excel.WorksheetDim rng As Excel.RangeDim strSheet As StringDim strPath As StringDim intRowCounter As IntegerDim intColumnCounter As IntegerDim msg As Outlook.MailItemDim nms As Outlook.NameSpaceDim fld As Outlook.MAPIFolderDim itm As ObjectstrSheet = &#8220;OutlookItems.xls&#8221;strPath = &#8220;C:Attendance&#8221;strSheet = strPath &amp; strSheet Debug.Print strSheet &#8216;Select &#8230; <a title=\"VBA code for export all email address and mail message from outlook to excel\" class=\"read-more\" href=\"https:\/\/99excel.com\/home\/vba-code-for-export-all-email-address-and-mail-message-from-outlook-to-excel\/\" aria-label=\"More on VBA code for export all email address and mail message from outlook to excel\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":1,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":897,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/115\/revisions\/897"}],"wp:attachment":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}