Hello and welcome to another Step-by-Step guide. In this, we will learn how to write a VB Script to Download Attachments from Outlook Classic. Refer to the following code.
SaveExcelAttachments_Simple.vbavb
1Option Explicit2Sub SaveExcelAttachments_Simple()3 Dim ns As Outlook.NameSpace4 Dim st As Outlook.store5 Dim root As Outlook.MAPIFolder6 Dim f1 As Outlook.MAPIFolder7 Dim f2 As Outlook.MAPIFolder8 Dim itms As Outlook.items9 Dim i As Long10 Dim mail As Outlook.MailItem11 Dim att As Outlook.Attachment12 Dim saveFolder As String13 Dim saved As Long, scanned As Long14 ' ==== Settings (adjust if needed) ====15 Const MAILBOX_NAME As String = ""16 Const FOLDER_PATH As String = "<Sub>" ' under the mailbox root17 saveFolder = "C:TempOutlookXlsx"18 ' =====================================19 Set ns = Application.GetNamespace("MAPI")20 ' Find mailbox root21 For Each st In ns.Stores22 If StrComp(st.DisplayName, MAILBOX_NAME, vbTextCompare) = 0 Then23 Set root = st.GetDefaultFolder(olFolderInbox).Parent24 Exit For25 End If26 Next st27 If root Is Nothing Then28 MsgBox "Mailbox not found: " & MAILBOX_NAME, vbExclamation29 Exit Sub30 End If31 ' Walk to the target folder 32 Set f1 = Nothing: Set f2 = Nothing33 On Error Resume Next34 Set f1 = root.Folders("")35 If Not f1 Is Nothing Then Set f2 = f1.Folders("")36 On Error GoTo 037 If f2 Is Nothing Then38 MsgBox "Folder not found: " & FOLDER_PATH, vbExclamation39 Exit Sub40 End If41 ' Ensure save directory exists42 CreateFolderIfMissing saveFolder43 ' Get items and sort (newest first)44 Set itms = f2.items45 itms.Sort "[ReceivedTime]", True46 ' Loop emails and save Excel attachments47 For i = 1 To itms.count48 If TypeOf itms(i) Is Outlook.MailItem Then49 Set mail = itms(i)50 scanned = scanned + 151 If mail.Attachments.count > 0 Then52 For Each att In mail.Attachments53 If IsExcelAttachment(att.fileName) Then54 Dim ts As String55 Dim cleanAttach As String56 Dim targetPath As String57 ts = Format(mail.ReceivedTime, "yyyy-mm-dd_hhmmss")58 cleanAttach = SanitizeFileName(att.fileName)59 targetPath = EnsureUniquePath(saveFolder, ts & " - " & cleanAttach)60 att.SaveAsFile targetPath61 saved = saved + 162 End If63 Next att64 End If65 End If66 Next i67 MsgBox "Done!" & vbCrLf & _68 "Folder: " & f2.folderPath & vbCrLf & _69 "Emails scanned: " & scanned & vbCrLf & _70 "Excel attachments saved: " & saved & vbCrLf & _71 "Saved to: " & saveFolder, vbInformation72End Sub73' --- Helpers ---74Private Function IsExcelAttachment(fileName As String) As Boolean75 Dim ext As String76 ext = LCase$(Mid$(fileName, InStrRev(fileName, ".") + 1))77 Select Case ext78 Case "xlsx", "xls", "xlsm", "xlsb", "csv"79 IsExcelAttachment = True80 Case Else81 IsExcelAttachment = False82 End Select83End Function84Private Function SanitizeFileName(ByVal s As String) As String85 ' Remove characters invalid for Windows filenames86 Dim badChars As Variant, ch As Variant87 badChars = Array("", "/", ":", "*", "?", """", "", "|")88 For Each ch In badChars89 s = Replace$(s, ch, " ")90 Next ch91 ' Collapse double spaces92 Do While InStr(s, " ") > 093 s = Replace$(s, " ", " ")94 Loop95 SanitizeFileName = Trim$(s)96End Function97Private Sub CreateFolderIfMissing(ByVal path As String)98 If Len(Dir$(path, vbDirectory)) = 0 Then99 MkDir path100 End If101End Sub102Private Function EnsureUniquePath(ByVal folder As String, ByVal fileName As String) As String103 ' If file exists, append (1), (2), ...104 Dim base As String, ext As String, p As Long, candidate As String105 Dim n As Long106 p = InStrRev(fileName, ".")107 If p > 0 Then108 base = Left$(fileName, p - 1)109 ext = Mid$(fileName, p) ' includes dot110 Else111 base = fileName112 ext = ""113 End If114 candidate = folder & "" & fileName115 n = 1116 Do While Len(Dir$(candidate)) > 0117 candidate = folder & "" & base & " (" & n & ")" & ext118 n = n + 1119 Loop120 EnsureUniquePath = candidate121End Function

