STEVENS3010
Board Regular
- Joined
- Feb 4, 2020
- Messages
- 89
- Office Version
- 365
- 2016
- Platform
- Windows
Hi all,
I currently have 3 separate vba codes which I've put into a 4th code to run all 3 as the same time. The code takes quite a long time to run and I was wondering if somebody could help me amend the code so that there is only 1 rather than 3, hopefully making it more 'code friendly' and hopefully making it quicker to run?
I'm afraid I'm a newbie with regards to vba and the codes I have put together so far I've done so from searching previous messages. Any help would be greatly appreciated. I've pasted the codes I'm currently using below...
Code 1
Code 2
Code 3
Code 4
I currently have 3 separate vba codes which I've put into a 4th code to run all 3 as the same time. The code takes quite a long time to run and I was wondering if somebody could help me amend the code so that there is only 1 rather than 3, hopefully making it more 'code friendly' and hopefully making it quicker to run?
I'm afraid I'm a newbie with regards to vba and the codes I have put together so far I've done so from searching previous messages. Any help would be greatly appreciated. I've pasted the codes I'm currently using below...
Code 1
VBA Code:
Sub getEmails()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Dim hdr As Variant
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("Dummy Folder")
Set olFldr = olFldr.Folders("Inbox")
Set olFldr = olFldr.Folders("Dummy Sub Folder")
Set olFldr = olFldr.Folders("Work 1 Folder")
ws.Cells.Clear
iRow = 2
Application.ScreenUpdating = False
For Each olItem In olFldr.Items
If olItem.Class = olMail Then
Set olMailItem = olItem
With olMailItem
ws.Cells(iRow, "A") = .Subject
ws.Cells(iRow, "B") = .ReceivedTime
ws.Cells(iRow, "C") = .Categories
iRow = iRow + 1
End With
End If
Next olItem
With ws
hdr = Array("Subject", "ReceicedTime", "Categories")
.Range("A1").Resize(, UBound(hdr)) = hdr
.Columns.AutoFit
End With
Application.ScreenUpdating = False
End Sub
Code 2
VBA Code:
Sub getEmails2()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Dim hdr As Variant
Set ws = ThisWorkbook.Worksheets("Sheet2")
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("Dummy Folder")
Set olFldr = olFldr.Folders("Inbox")
Set olFldr = olFldr.Folders(" Dummy Sub Folder")
Set olFldr = olFldr.Folders("Work 2 Folder")
ws.Cells.Clear
iRow = 2
Application.ScreenUpdating = False
For Each olItem In olFldr.Items
If olItem.Class = olMail Then
Set olMailItem = olItem
With olMailItem
ws.Cells(iRow, "A") = .Subject
ws.Cells(iRow, "B") = .ReceivedTime
ws.Cells(iRow, "C") = .Categories
iRow = iRow + 1
End With
End If
Next olItem
With ws
hdr = Array("Subject", "ReceicedTime", "Categories")
.Range("A1").Resize(, UBound(hdr)) = hdr
.Columns.AutoFit
End With
Application.ScreenUpdating = False
End Sub
Code 3
VBA Code:
Sub getEmails3()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim ws As Worksheet
Dim iRow As Long
Dim hdr As Variant
Set ws = ThisWorkbook.Worksheets("Sheet3")
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("Dummy Folder")
Set olFldr = olFldr.Folders("Inbox")
Set olFldr = olFldr.Folders("Dummy Sub Folder")
Set olFldr = olFldr.Folders("Work 3 Folder")
ws.Cells.Clear
iRow = 2
Application.ScreenUpdating = False
For Each olItem In olFldr.Items
If olItem.Class = olMail Then
Set olMailItem = olItem
With olMailItem
ws.Cells(iRow, "A") = .Subject
ws.Cells(iRow, "B") = .ReceivedTime
ws.Cells(iRow, "C") = .Categories
iRow = iRow + 1
End With
End If
Next olItem
With ws
hdr = Array("Subject", "ReceicedTime", "Categories")
.Range("A1").Resize(, UBound(hdr)) = hdr
.Columns.AutoFit
End With
Application.ScreenUpdating = False
End Sub
Code 4
VBA Code:
Sub getemailsfromoutlook()
Call getEmails
Call getEmails2
Call getEmails3
End Sub