Hi,
I have code for a macro so that I could get data from Outlook.
I can hardcode where I get the data from with:
However, I was wondering if it was possible to make some kind of button or form so that I can select a folder (Inbox, Sent Items, Archive, etc.) and to select a date range (e.g., if I wanted to see the info for the emails I sent during the last two weeks instead of ALL the emails in the Sent Items folder).
Would it be easier to do this as a macro from Outlook (export data to Excel) vs. a macro in Excel (import data from Outlook)?
Thanks!
I have code for a macro so that I could get data from Outlook.
Code:
Option Explicit
Dim arrData() As Variant
Dim Cnt As Long
Sub test()
Dim olApp As Object
Dim olNS As Object
Dim olFldr As Object
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.GetDefaultFolder(5) '5=olSentMail
Cnt = 0
Call RecursiveFolders(olFldr)
Cells.ClearContents
With ActiveSheet.Range("a1").Resize(, 5)
.Value = Array("Folder", "To", "Subject", "Importance", "Sent")
.Font.Bold = True
End With
ActiveSheet.Range("A2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = WorksheetFunction.Transpose(arrData)
ActiveSheet.Range("E2", Range("E2").End(xlDown)).NumberFormat = "mm/dd/yyyy h:mm AM/PM"
ActiveSheet.Columns.AutoFit
End Sub
Sub RecursiveFolders(olFolder As Object)
Dim olSubFolder As Object
Dim olMail As Object
For Each olMail In olFolder.Items
Cnt = Cnt + 1
ReDim Preserve arrData(1 To 5, 1 To Cnt)
arrData(1, Cnt) = olFolder.FolderPath
arrData(2, Cnt) = olMail.To
arrData(3, Cnt) = olMail.Subject
arrData(4, Cnt) = olMail.Importance
arrData(5, Cnt) = olMail.SentOn
Next
For Each olSubFolder In olFolder.Folders
Call RecursiveFolders(olSubFolder)
Next olSubFolder
End Sub
I can hardcode where I get the data from with:
Code:
Set olFldr = olNS.GetDefaultFolder(5) '5=olFolderSentMail, 6=olFolderInbox
However, I was wondering if it was possible to make some kind of button or form so that I can select a folder (Inbox, Sent Items, Archive, etc.) and to select a date range (e.g., if I wanted to see the info for the emails I sent during the last two weeks instead of ALL the emails in the Sent Items folder).
Would it be easier to do this as a macro from Outlook (export data to Excel) vs. a macro in Excel (import data from Outlook)?
Thanks!