OTPM
12-13-2015, 05:56 AM
Hi
I am struggling with trying to working out the code to extract mails from a designated Mailbox and Inbox together with all mails from each subfolder. The code I have works well with each individual folder but I have been struggling to find something I can add to my code the work through each sub-folder as well. I also want to specify a particular Mailbox and Inbox within the code. My current code is shown below and any help would be appreciated:
Option Explicit
Sub RMGpEmailExport()
Dim xlApp As Object
Dim xlWb As Object
Dim xlSheet As Object
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder
Dim mailitems As Outlook.Items
Dim currentExplorer As Explorer
Dim Selection As Selection
Dim olItem As Outlook.mailItem
Dim obj As Object
Dim strColA, strColB, strColC, strColD, strColE, strColF, strColG, strColH, strColI, StrColJ, StrColK, StrColL, StrColM, StrColN, StrColO, StrColP, StrColQ As String
Dim LDate As Date
Dim propertyAccessor As Outlook.propertyAccessor
Dim PropName As String
Dim LVE As String
Dim LVET As String 'Date
Dim dtUTC As Date
Dim dtLocal As Date
Dim v As Variant
Dim strType As String
Dim olRecip As Recipient
Set olApp = New Outlook.Application
Set olSession = olApp.GetNamespace("MAPI")
Set olStartFolder = olSession.PickFolder
Set mailitems = olStartFolder.Items
' Get Excel set up
'enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = "D:\Email Metrics\RM Group\test.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWb = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWb.Sheets("RMGpRawData")
' Process the message record
On Error Resume Next
'Find the next empty line of the worksheet
'Range(coltoSearch & Rows.count).End(xlUp).Row
Dim NextRow As Integer
rCount = xlSheet.Range("A100000").End(xlUp).Row + 1
' get the values from outlook
Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
For Each obj In Selection
Set olItem = obj
If olItem <> "nothing" Then
'collect the fields
strColA = olItem.SenderName
strColB = olItem.CreationTime
strColC = olItem.To
strColD = olItem.Recipients
strColE = olItem.ReceivedByName
strColF = olItem.SentOn
strColG = olItem.ReceivedTime
strColH = olItem.UnRead
strColI = olItem.LastModificationTime
StrColJ = olItem.UserProperties
StrColK = olItem.Categories
'PR_LAST_VERB_EXECUTED
'URL LINKS REMOVED TO ALLOW ME TO POST.................................
'Obtain an instance of PropertyAccessor class
Set propertyAccessor = olItem.propertyAccessor
'Call GetProperty
LVE = propertyAccessor.GetProperty(LVE)
StrColL = LVE
'PR_LAST_VERB_EXECUTED_TIME
'URL LINKS REMOVED TO ALLOW ME TO POST.................................................
'Obtain an instance of PropertyAccessor class
Set propertyAccessor = olItem.propertyAccessor
'Call GetProperty
v = propertyAccessor.GetProperty(LVET)
'LVET = propertyAccessor.GetProperty(LVET)
StrColM = v
StrColN = olItem.ConversationID
StrColO = olItem.ConversationIndex
StrColP = olItem.ConversationTopic
StrColQ = olRecip.Type
'Add column Headers to the Excel Extract
xlSheet.Range("A" & 1) = "Sender Name"
xlSheet.Range("B" & 1) = "Creation Time"
xlSheet.Range("C" & 1) = "Sent To"
xlSheet.Range("D" & 1) = "Recipients"
xlSheet.Range("E" & 1) = "Received By Name"
xlSheet.Range("F" & 1) = "Sent On"
xlSheet.Range("G" & 1) = "Received Time"
xlSheet.Range("H" & 1) = "UnRead"
xlSheet.Range("I" & 1) = "Last Modification Time"
xlSheet.Range("J" & 1) = "User Properties"
xlSheet.Range("K" & 1) = "Categories"
xlSheet.Range("L" & 1) = "Last Verb Executed"
xlSheet.Range("M" & 1) = "Last Verb Executed Time"
xlSheet.Range("N" & 1) = "Conversation ID"
xlSheet.Range("O" & 1) = "Conversation Index"
xlSheet.Range("P" & 1) = "Conversation Topic"
xlSheet.Range("Q" & 1) = "Recipient Type"
'write them in the excel sheet
xlSheet.Range("A" & rCount) = strColA
xlSheet.Range("B" & rCount) = strColB
xlSheet.Range("C" & rCount) = strColC
xlSheet.Range("D" & rCount) = strColD
xlSheet.Range("E" & rCount) = strColE
xlSheet.Range("F" & rCount) = strColF
xlSheet.Range("G" & rCount) = strColG
xlSheet.Range("H" & rCount) = strColH
xlSheet.Range("I" & rCount) = strColI
xlSheet.Range("J" & rCount) = StrColJ
xlSheet.Range("K" & rCount) = StrColK
xlSheet.Range("L" & rCount) = StrColL
xlSheet.Range("M" & rCount) = StrColM
xlSheet.Range("N" & rCount) = StrColN
xlSheet.Range("O" & rCount) = StrColO
xlSheet.Range("P" & rCount) = StrColP
xlSheet.Range("Q" & rCount) = StrColQ
'Next row
rCount = rCount + 1
End If
Next
xlWb.Save
xlWb.Close 1
If bXStarted Then
xlApp.Quit
End If
Set olItem = Nothing
Set obj = Nothing
Set currentExplorer = Nothing
Set xlApp = Nothing
Set xlWb = Nothing
Set xlSheet = Nothing
MsgBox ("All RM Group Emails exported to Excel..........")
End Sub
I am struggling with trying to working out the code to extract mails from a designated Mailbox and Inbox together with all mails from each subfolder. The code I have works well with each individual folder but I have been struggling to find something I can add to my code the work through each sub-folder as well. I also want to specify a particular Mailbox and Inbox within the code. My current code is shown below and any help would be appreciated:
Option Explicit
Sub RMGpEmailExport()
Dim xlApp As Object
Dim xlWb As Object
Dim xlSheet As Object
Dim rCount As Long
Dim bXStarted As Boolean
Dim enviro As String
Dim strPath As String
Dim olApp As Outlook.Application
Dim olSession As Outlook.NameSpace
Dim olStartFolder As Outlook.MAPIFolder
Dim mailitems As Outlook.Items
Dim currentExplorer As Explorer
Dim Selection As Selection
Dim olItem As Outlook.mailItem
Dim obj As Object
Dim strColA, strColB, strColC, strColD, strColE, strColF, strColG, strColH, strColI, StrColJ, StrColK, StrColL, StrColM, StrColN, StrColO, StrColP, StrColQ As String
Dim LDate As Date
Dim propertyAccessor As Outlook.propertyAccessor
Dim PropName As String
Dim LVE As String
Dim LVET As String 'Date
Dim dtUTC As Date
Dim dtLocal As Date
Dim v As Variant
Dim strType As String
Dim olRecip As Recipient
Set olApp = New Outlook.Application
Set olSession = olApp.GetNamespace("MAPI")
Set olStartFolder = olSession.PickFolder
Set mailitems = olStartFolder.Items
' Get Excel set up
'enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = "D:\Email Metrics\RM Group\test.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWb = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWb.Sheets("RMGpRawData")
' Process the message record
On Error Resume Next
'Find the next empty line of the worksheet
'Range(coltoSearch & Rows.count).End(xlUp).Row
Dim NextRow As Integer
rCount = xlSheet.Range("A100000").End(xlUp).Row + 1
' get the values from outlook
Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
For Each obj In Selection
Set olItem = obj
If olItem <> "nothing" Then
'collect the fields
strColA = olItem.SenderName
strColB = olItem.CreationTime
strColC = olItem.To
strColD = olItem.Recipients
strColE = olItem.ReceivedByName
strColF = olItem.SentOn
strColG = olItem.ReceivedTime
strColH = olItem.UnRead
strColI = olItem.LastModificationTime
StrColJ = olItem.UserProperties
StrColK = olItem.Categories
'PR_LAST_VERB_EXECUTED
'URL LINKS REMOVED TO ALLOW ME TO POST.................................
'Obtain an instance of PropertyAccessor class
Set propertyAccessor = olItem.propertyAccessor
'Call GetProperty
LVE = propertyAccessor.GetProperty(LVE)
StrColL = LVE
'PR_LAST_VERB_EXECUTED_TIME
'URL LINKS REMOVED TO ALLOW ME TO POST.................................................
'Obtain an instance of PropertyAccessor class
Set propertyAccessor = olItem.propertyAccessor
'Call GetProperty
v = propertyAccessor.GetProperty(LVET)
'LVET = propertyAccessor.GetProperty(LVET)
StrColM = v
StrColN = olItem.ConversationID
StrColO = olItem.ConversationIndex
StrColP = olItem.ConversationTopic
StrColQ = olRecip.Type
'Add column Headers to the Excel Extract
xlSheet.Range("A" & 1) = "Sender Name"
xlSheet.Range("B" & 1) = "Creation Time"
xlSheet.Range("C" & 1) = "Sent To"
xlSheet.Range("D" & 1) = "Recipients"
xlSheet.Range("E" & 1) = "Received By Name"
xlSheet.Range("F" & 1) = "Sent On"
xlSheet.Range("G" & 1) = "Received Time"
xlSheet.Range("H" & 1) = "UnRead"
xlSheet.Range("I" & 1) = "Last Modification Time"
xlSheet.Range("J" & 1) = "User Properties"
xlSheet.Range("K" & 1) = "Categories"
xlSheet.Range("L" & 1) = "Last Verb Executed"
xlSheet.Range("M" & 1) = "Last Verb Executed Time"
xlSheet.Range("N" & 1) = "Conversation ID"
xlSheet.Range("O" & 1) = "Conversation Index"
xlSheet.Range("P" & 1) = "Conversation Topic"
xlSheet.Range("Q" & 1) = "Recipient Type"
'write them in the excel sheet
xlSheet.Range("A" & rCount) = strColA
xlSheet.Range("B" & rCount) = strColB
xlSheet.Range("C" & rCount) = strColC
xlSheet.Range("D" & rCount) = strColD
xlSheet.Range("E" & rCount) = strColE
xlSheet.Range("F" & rCount) = strColF
xlSheet.Range("G" & rCount) = strColG
xlSheet.Range("H" & rCount) = strColH
xlSheet.Range("I" & rCount) = strColI
xlSheet.Range("J" & rCount) = StrColJ
xlSheet.Range("K" & rCount) = StrColK
xlSheet.Range("L" & rCount) = StrColL
xlSheet.Range("M" & rCount) = StrColM
xlSheet.Range("N" & rCount) = StrColN
xlSheet.Range("O" & rCount) = StrColO
xlSheet.Range("P" & rCount) = StrColP
xlSheet.Range("Q" & rCount) = StrColQ
'Next row
rCount = rCount + 1
End If
Next
xlWb.Save
xlWb.Close 1
If bXStarted Then
xlApp.Quit
End If
Set olItem = Nothing
Set obj = Nothing
Set currentExplorer = Nothing
Set xlApp = Nothing
Set xlWb = Nothing
Set xlSheet = Nothing
MsgBox ("All RM Group Emails exported to Excel..........")
End Sub