按日期从文件夹中打开 3 个文件
open 3 files from folder by date
我想在 corel draw 中按日期打开文件夹中的 3 个文件。我找到一个宏并修改但只打开一个文件
Sub openLastModified()
Dim folderPath As String, tableName As String, latestTblName As String
Dim modifiedDate As Date
folderPath = "C:\test\"
tableName = Dir(folderPath & "*.cdr")
Do While tableName <> vbNullString
modifiedDate = FileDateTime(folderPath & tableName)
If latestModified < modifiedDate Then
latestModified = modifiedDate
latestTblName = tableName
End If
tableName = Dir()
Loop
OpenDocument folderPath & latestTblName
End Sub
您似乎想打开 C:/test/ 目录中的三个 最近修改的 文件。
最简洁的方法是将文件名及其各自的修改日期加载到数组中,按修改日期对它们进行排序,然后从数组底部加载三个。还有其他 answers on Stack Overflow 可以帮助您有效地对数组进行排序。
遗憾的是,VBA 不提供任何简单的内置排序功能。一种不太干净的方法是将文件名及其各自的修改日期加载到工作表中,然后利用 Excel's sorting functions,再次读取排序范围的底部。
现在,如果您只对最近修改的三个感兴趣并且永远只会对 三个 感兴趣,这里是对现有代码的快速修改:
Sub openLastModified()
Dim folderPath As String, tableName As String, latestTblName(2) As String
Dim modifiedDate As Date
Dim latestModified(2) As Date
folderPath = "C:\test\"
tableName = Dir(folderPath & "*.cdr")
Do While tableName <> vbNullString
Dim i As Long
modifiedDate = FileDateTime(folderPath & tableName)
For i = 0 To 2
' Check if this file's modification date is later than that of each
' in the latestTblName array, starting with the most recent.
If latestModified(i) < modifiedDate Then
Dim j As Long
' Move remaining values down in the array.
For j = 1 To i Step -1
latestModified(j + 1) = latestModified(j)
latestTblName(j + 1) = latestTblName(j)
Next j
' Place the file name & modification date in the arrays.
latestModified(i) = modifiedDate
latestTblName(i) = tableName
Exit For
End If
Next i
tableName = Dir()
Loop
For i = 0 To 2
OpenDocument folderPath & latestTblName(i)
Next i
End Sub
我想在 corel draw 中按日期打开文件夹中的 3 个文件。我找到一个宏并修改但只打开一个文件
Sub openLastModified()
Dim folderPath As String, tableName As String, latestTblName As String
Dim modifiedDate As Date
folderPath = "C:\test\"
tableName = Dir(folderPath & "*.cdr")
Do While tableName <> vbNullString
modifiedDate = FileDateTime(folderPath & tableName)
If latestModified < modifiedDate Then
latestModified = modifiedDate
latestTblName = tableName
End If
tableName = Dir()
Loop
OpenDocument folderPath & latestTblName
End Sub
您似乎想打开 C:/test/ 目录中的三个 最近修改的 文件。
最简洁的方法是将文件名及其各自的修改日期加载到数组中,按修改日期对它们进行排序,然后从数组底部加载三个。还有其他 answers on Stack Overflow 可以帮助您有效地对数组进行排序。
遗憾的是,VBA 不提供任何简单的内置排序功能。一种不太干净的方法是将文件名及其各自的修改日期加载到工作表中,然后利用 Excel's sorting functions,再次读取排序范围的底部。
现在,如果您只对最近修改的三个感兴趣并且永远只会对 三个 感兴趣,这里是对现有代码的快速修改:
Sub openLastModified()
Dim folderPath As String, tableName As String, latestTblName(2) As String
Dim modifiedDate As Date
Dim latestModified(2) As Date
folderPath = "C:\test\"
tableName = Dir(folderPath & "*.cdr")
Do While tableName <> vbNullString
Dim i As Long
modifiedDate = FileDateTime(folderPath & tableName)
For i = 0 To 2
' Check if this file's modification date is later than that of each
' in the latestTblName array, starting with the most recent.
If latestModified(i) < modifiedDate Then
Dim j As Long
' Move remaining values down in the array.
For j = 1 To i Step -1
latestModified(j + 1) = latestModified(j)
latestTblName(j + 1) = latestTblName(j)
Next j
' Place the file name & modification date in the arrays.
latestModified(i) = modifiedDate
latestTblName(i) = tableName
Exit For
End If
Next i
tableName = Dir()
Loop
For i = 0 To 2
OpenDocument folderPath & latestTblName(i)
Next i
End Sub