Hello, I have a VBA macro in my Outlook, which takes around 60 minutes to process around 45k emails. I was wondering, whether there is a way to somehow speed it up. I'd be very grateful for some suggestions. Here's the code:
Code:
Option Explicit
Option Compare Text
'References : Microsoft Outlook 16.0 Object Library
'--------------------------------------------------
Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long
Const NoError = 0
Sub SearchandDownload()
On Error Resume Next
Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim atmt As attachment
Dim fileName As String
Dim i As Long
Dim itemsCount As Long
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
itemsCount = Inbox.Items.Count
If itemsCount = 0 Then
MsgBox "There are no valid emails in your Inbox.", vbInformation, "Nothing Found"
Exit Sub
End If
For Each Item In Inbox.Items
If InStr(Item.Body, "pass") > 0 Or InStr(Item.Body, "certifi") > 0 Or InStr(Item.Body, "licen") > 0 Or InStr(Item.Body, "secur") > 0 _
Or InStr(Item.Body, "acco") > 0 Then
For Each atmt In Item.Attachments
If atmt.Size > 45000 Then
If fileName = "" Then
Call CreateFolder
End If
fileName = MyDocs() & Item.SenderName & " " & atmt.fileName
atmt.SaveAsFile fileName
i = i + 1
End If
Next atmt
End If
For Each atmt In Item.Attachments
If InStr(atmt.fileName, "port") > 0 Or InStr(atmt.fileName, "certifie") > 0 Or InStr(atmt.fileName, "lice") > 0 Or InStr(atmt.fileName, "secu") > 0 _
Or InStr(atmt.fileName, "accou") > 0 Then
If fileName = "" Then
Call CreateFolder
End If
fileName = MyDocs() & Item.SenderName & " " & atmt.fileName
atmt.SaveAsFile fileName
i = i + 1
End If
If Right(atmt.fileName, 3) = "pdf" Or Right(atmt.fileName, 3) = "jpg" And atmt.Size > 45000 Or Right(atmt.fileName, 3) = "JPG" And atmt.Size > 45000 Then
If fileName = "" Then
Call CreateFolder
End If
fileName = MyDocs() & Item.SenderName & " " & atmt.fileName
atmt.SaveAsFile fileName
i = i + 1
End If
Next atmt
Next Item
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
If i > 0 Then
MsgBox "The attached files detected were saved into the Email Attachments folder in My Documents. This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
Else
MsgBox "There are no attached files in your Inbox.", vbInformation, "Finished!"
End If
End Sub
Function GetUserName()
Const lpnLength As Integer = 255
Dim status As Integer
Dim lpName As String
Dim lpUserName As String
lpUserName = Space$(lpnLength + 1)
status = WNetGetUser(lpName, lpUserName, lpnLength)
If status = NoError Then
lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
Else
MsgBox "Unable To Get The Name", vbExclamation
End
End If
GetUserName = lpUserName
End Function
Function MyDocs() As String
Dim strStart As String
Dim strEnd As String
Dim strUser As String
strUser = GetUserName()
strStart = "C:\Documents and Settings\"
strEnd = "\My Documents\Email Attachments\"
MyDocs = strStart & strUser & strEnd
End Function
Private Sub CreateFolder()
Dim wsh As Object
Dim fs As Object
Dim destFolder As String
Dim myDocPath As String
If destFolder = "" Then
Set wsh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
myDocPath = wsh.SpecialFolders.Item("mydocuments")
destFolder = myDocPath & "\Email Attachments"
If Not fs.FolderExists(destFolder) Then
fs.CreateFolder destFolder
End If
End If
End Sub