Hello
I have created new workbook and copied your code and added some lines to make progress bar work on your file
I have deleted all comments and added new comments '>> Added at those lines added to the code
'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 GetEmailAttachments()
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 '>> Added
Dim x As Long '>> Added
Dim pct As Single '>> Added
ufProgress.LabelProgress.Width = 0 '>> Added
ufProgress.Show '>> Added
Set ns = GetNamespace("MAPI")
Set inbox = ns.GetDefaultFolder(olFolderInbox)
i = 0
itemsCount = inbox.Items.Count '>> Added
If itemsCount = 0 Then
MsgBox "There Are No Messages In The Inbox.", vbInformation, "Nothing Found"
Exit Sub
End If
For Each item In inbox.Items
'>> Added This Portion
'=====================
x = x + 1
pct = x / itemsCount
With ufProgress
.LabelCaption.Caption = "Processing Row " & x & " Of " & itemsCount
.LabelProgress.Width = pct * (.FrameProgress.Width)
End With
DoEvents
'=====================
For Each atmt In item.Attachments
If Right(atmt.fileName, 3) = "xls" Or Right(atmt.fileName, 3) = "pdf" Or Right(atmt.fileName, 3) = "txt" Then
If fileName = "" Then
Call CreateFolder
End If
fileName = MyDocs() & item.SenderName & " " & atmt.fileName
atmt.SaveAsFile fileName
i = i + 1
End If
Next atmt
If x = itemsCount Then Unload ufProgress '>> Added
Next item
If i > 0 Then
MsgBox "There Are " & i & " Attached Files." & vbCrLf & "They Were Saved Into The Email Attachments Folder In My Documents.", vbInformation, "Finished!"
Else
MsgBox "There Are No Attached Files In Your Mail.", vbInformation, "Finished!"
End If
GetAttachments_exit:
Set atmt = Nothing
Set item = Nothing
Set ns = Nothing
Exit Sub
GetAttachments_err:
MsgBox "An Unexpected Error Has Occurred." _
& vbCrLf & "Please Note And Report The Following Information." _
& vbCrLf & "Macro Name: GetEmailAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit
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
Hope that helps you