按日期从文件夹中打开3个文件

时间:2019-03-31 13:33:42

标签: vba coreldraw

我想在corel绘图中按日期打开文件夹中的3个文件。我找到了一个宏并进行了修改,但只打开了一个文件

let yAngle = atan2(dirVector.x, dirVector.z)

1 个答案:

答案 0 :(得分:0)

您似乎想打开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