VBA Outlook - Create New Fold on Hard Drive - Based on the first 5 Characters of .xls

brian1988

New Member
Joined
Jan 8, 2015
Messages
1
I found this code online, which works great for moving all excel files from my outlook subfolder, into a folder I have already have created on my C drive.

I was wondering if there is a way to create a new folder on my C Drive based on the excel files in my subfolder.

If the excel files in my Outlook subfolder "DataExtract" have the same first 5 characters to the excel document "?????.xls" then it will create a new folder on my C drive (with the folder name being the first 5 characters) and will save all of the excel files with the first 5 characters into the new file created on the C Drive.

Any help would be greatly appreciated!!


Code:
Option Explicit
Const FolderPath = "c:\Folder\"
Sub GetSpreadSheets()
On Error Resume Next
Dim ns As NameSpace
Set ns = GetNamespace("MAPI")
Dim Inbox As MAPIFolder
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
  
Dim searchFolder As String
searchFolder = InputBox("Search for Reports?")


Dim Subfolder As MAPIFolder
  
Dim Item As Object
Dim Attach As Attachment
Dim FileName As String
Dim i As Integer


 
 
 If searchFolder <> "inbox" Then
Set Subfolder = Inbox.Folders(searchFolder)
            i = 0
            If Subfolder.Items.Count = 0 Then
               MsgBox "There are no messages in the Inbox.", vbInformation, _
                      "Nothing Found"
               Exit Sub
            End If
                    For Each Item In Subfolder.Items
                       For Each Attach In Item.Attachments
                            If Right(Attach.FileName, 3) = "xls" Then
                                FileName = "C:\Email Attachments\" & Attach.FileName
                                Attach.SaveAsFile (FolderPath & Attach.FileName)
                          
                          i = i + 1
                          End If
                       Next Attach
                    Next Item
                    
                    '==============================================================================
                    'to search specific type of file:
'                               'For Each Item In Inbox.Items
'                               For Each Atmt In Item.Attachments
'                                  If Right(Atmt.FileName, 3) = "xls" Then
'                                     FileName = "C:\Email Attachments\" & Atmt.FileName
'                                     Atmt.SaveAsFile FileName
'                                     i = i + 1
'                                  End If
'                               Next Atmt
'                            Next Item
                    '===============================================================================
                    
        Else
         i = 0
            If Inbox.Items.Count = 0 Then
               MsgBox "There are no messages in the Inbox.", vbInformation, _
                      "Nothing Found"
               Exit Sub
            End If
            On Error Resume Next
            For Each Item In Inbox.Items
               For Each Attach In Item.Attachments
                  FileName = FolderPath & Attach.FileName
                  Attach.SaveAsFile FileName
                  i = i + 1
               Next Attach
            Next Item
     End If
     
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,215,389
Messages
6,124,665
Members
449,178
Latest member
Emilou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top