In the line 'Set files = folder.files' I am getting a Type Mismatch error within; '…
- User
In the line 'Set files = folder.files' I am getting a Type Mismatch error within; ' Function to get files sorted by date created in a folder Function GetFilesSortedByDateCreated(folderPath As String, ascendingOrder As Boolean) As Collection Dim fs As Object ' Scripting.FileSystemObject Dim folder As Scripting.folder Dim file As Object ' Scripting.File Dim files As Collection Dim sortedFiles As Collection Dim fileDateCreated As Date Dim i As Long
folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup" Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(folderPath) Set files = folder.files ' Add file paths to collection Set sortedFiles = New Collection For Each file In files sortedFiles.Add file.path Next file ' Sort collection based on file date created For i = 1 To sortedFiles.count - 1 For j = i To sortedFiles.count If (ascendingOrder And FileDateTime(sortedFiles(j)) < FileDateTime(sortedFiles(i))) Or _ (Not ascendingOrder And FileDateTime(sortedFiles(j)) > FileDateTime(sortedFiles(i))) Then sortedFiles.Add sortedFiles(i), Before:=j sortedFiles.Remove i End If Next j Next i Set GetFilesSortedByDateCreated = sortedFilesEnd Function
- Assistant
The Type Mismatch error occurs because the "files" variable is declared as a Collection, but you are trying to assign it a value of type Scripting.Files.
To fix this error, you need to change the declaration of the "files" variable to Scripting.Files instead of Collection. The modified code would look like this:
Function GetFilesSortedByDateCreated(folderPath As String, ascendingOrder As Boolean) As Collection Dim fs As Object ' Scripting.FileSystemObject Dim folder As Scripting.folder Dim file As Object ' Scripting.File Dim files As Scripting.Files Dim sortedFiles As Collection Dim fileDateCreated As Date Dim i As Long folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup" Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(folderPath) Set files = folder.files ' Add file paths to collection Set sortedFiles = New Collection For Each file In files sortedFiles.Add file.path Next file ' Sort collection based on file date created For i = 1 To sortedFiles.count - 1 For j = i To sortedFiles.count If (ascendingOrder And FileDateTime(sortedFiles(j)) < FileDateTime(sortedFiles(i))) Or _ (Not ascendingOrder And FileDateTime(sortedFiles(j)) > FileDateTime(sortedFiles(i))) Then sortedFiles.Add sortedFiles(i), Before:=j sortedFiles.Remove i End If Next j Next i Set GetFilesSortedByDateCreated = sortedFiles End Function - User
I am getting an error,'Object variable or With block variable not set' on the line 'oldestBackupFiles.Add backupFile' in the the event below; ' Sub to delete the oldest backups in a folder, up to a specified count Sub DeleteOldestBackups(folderPath As String, count As Long) Dim backupFiles As Collection Dim oldestBackupFiles As Collection Dim backupFile As Variant Dim i As Long
folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup" ' Get all backup files sorted by date created in ascending order Set backupFiles = GetFilesSortedByDateCreated(folderPath, True) ' Delete the oldest backup files, up to the specified count i = 0 For Each backupFile In backupFiles If UCase(Right(backupFile, 5)) = ".XLSM" Then If i < count Then ' Add backup file to collection of oldest backup files oldestBackupFiles.Add backupFile End If i = i + 1 End If Next backupFile ' Delete the oldest backup files For Each backupFile In oldestBackupFiles Kill backupFile Next backupFileEnd Sub
- Assistant
The "Object variable or With block variable not set" error occurs because you are trying to add a value to the "oldestBackupFiles" collection, but you haven't initialized it before adding the value.
To fix this error, you need to initialize the "oldestBackupFiles" collection by adding the line
Set oldestBackupFiles = New Collectionbefore adding the backup file to the collection. The modified code would look like this:Sub DeleteOldestBackups(folderPath As String, count As Long) Dim backupFiles As Collection Dim oldestBackupFiles As Collection Dim backupFile As Variant Dim i As Long folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup" ' Get all backup files sorted by date created in ascending order Set backupFiles = GetFilesSortedByDateCreated(folderPath, True) ' Delete the oldest backup files, up to the specified count i = 0 Set oldestBackupFiles = New Collection ' Initialize the collection For Each backupFile In backupFiles If UCase(Right(backupFile, 5)) = ".XLSM" Then If i < count Then ' Add backup file to collection of oldest backup files oldestBackupFiles.Add backupFile End If i = i + 1 End If Next backupFile ' Delete the oldest backup files For Each backupFile In oldestBackupFiles Kill backupFile Next backupFile End Sub - User
- Assistant
The error "Runtime error 1004 - Backup folder path cannot be found" is occurring because the "backupPath" variable is being overwritten with an incorrect value.
In the
DeleteOldestBackupssubroutine, you are reassigning the value of "folderPath" to a hard-coded path:folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup"You need to remove this line from both the
DeleteOldestBackupsandGetFilesSortedByDateCreatedsubroutines to ensure that the correct backup folder path is being used.Additionally, the error could also be caused by improper concatenation of the backupPath variable in the following code:
backupPath = backupPath & "Service Providers_" & Format(Now, "ddmmyy_hhmmss") & ".xlsm"To ensure the correct concatenation, you need to include a path separator (\ or /) before the "Service Providers_" filename. For example:
backupPath = backupPath & "\" & "Service Providers_" & Format(Now, "ddmmyy_hhmmss") & ".xlsm"Make sure to update these sections in your code and try again.