0

我有以下代码,用于打开最后修改的 CSV 文件,并且实际上只是更改了路径名和扩展名,但它现在不起作用,希望能提供任何关于我哪里出错的指针:

我正在使用的代码:

Sub ReceiptTest()

    On Error Resume Next
    With Application.FileSearch
    .LookIn = "\\K123456\shared\IT Public\ReceiptsETE\Archive\": .Filename = "*.XLS*"
    .Execute msoSortByLastModified, msoSortOrderDescending
    For FF = 1 To .FoundFiles.Count
    If FileDateTime(.FoundFiles(FF)) > LastModDate Then
    LastModDate = FileDateTime(.FoundFiles(FF))
    lmf = .FoundFiles(FF)
    End If
    Next
    End With
    Workbooks.Open (lmf)

    End Sub

谢谢

4

3 回答 3

3

如果您尝试打开 CSV,那么您的文件名应该是.csv,而不是 xls。这是我的做法。您需要设置对 Microsoft Scripting Runtime 的引用。即使您从 2003 升级,它也能正常工作

Sub OpenCSV()

    Dim sFldr As String
    Dim fso As Scripting.FileSystemObject
    Dim fsoFile As Scripting.File
    Dim fsoFldr As Scripting.Folder
    Dim dtNew As Date, sNew As String

    Const sCSVTYPE As String = "Microsoft Office Excel Comma Separated Values File"

    Set fso = New Scripting.FileSystemObject

    sFldr = "C:\Documents and Settings\dick\My Documents\QBExport\"

    Set fsoFldr = fso.GetFolder(sFldr)

    For Each fsoFile In fsoFldr.Files
        If fsoFile.DateLastModified > dtNew And fsoFile.Type = sCSVTYPE Then
            sNew = fsoFile.Path
            dtNew = fsoFile.DateLastModified
        End If
    Next fsoFile

    Workbooks.Open sNew

End Sub
于 2013-05-31T12:57:50.340 回答
0

我无法测试您的代码,因为我使用的是 Execl 2010 并且不支持 Application.FileSearch。

我用它来查找最新修改的文​​件...

Sub GetLatestFile()

Dim strFolder  As String
Dim strFile    As String
Dim latestFile As String
Dim dtLast     As Date

'   assign variables
    strFolder = "C:\" 'The end of this path must have a \ on it
    strFile = Dir(strFolder & "\*.*", vbNormal)    ' Any File
'   strFile = Dir(strFolder & "\*.xls*", vbNormal) ' Excel Files
'   strFile = Dir(strFolder & "\*.csv", vbNormal)  ' CSV Files

'   loop through files to find latest modified date
    Do While strFile <> ""
        If FileDateTime(strFolder & strFile) > dtLast Then
            dtLast = FileDateTime(strFolder & strFile)
            latestFile = strFolder & strFile
        End If
        strFile = Dir
    Loop

    MsgBox latestFile

End Sub
于 2013-05-31T11:00:19.327 回答
0

好的,我使用上面的 Conman 的代码进行了一些修改(我对他的代码进行了更改,如果他们获得批准,它们将得到反映)。这是他进行这些更改的代码:

Sub GetLatestFile()

Dim strFolder  As String
Dim strFile    As String
Dim latestFile As String
Dim dtLast     As Date

'   assign variables
    strFolder = "C:\your\file\path\goes\here\" 'the path of the file drop folder (you need the final "\" on the directory
    strFile = Dir(strFolder & "\*.xls*", vbNormal) ' Excel Files
'   strFile = Dir(strFolder & "\*.csv", vbNormal)  ' CSV Files
'   strFile = Dir(strFolder & "\*.*", vbNormal)    ' Any File

'   loop through files to find latest modified date
    Do While strFile <> ""
        If FileDateTime(strFolder & strFile) > dtLast Then
            dtLast = FileDateTime(strFolder & strFile)
            latestFile = strFolder & strFile
        End If
        strFile = Dir
    Loop

    MsgBox latestFile

End Sub

您还可以使用文件对话框设置 strFolder 并将其传递到上述子目录中。这是一个例子:

Sub ChooseFolder()
    Dim fd As Office.FileDialog
    Dim strFolder As String

    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        If .Show Then
            strFolder = .SelectedItems(1)
        End If
    End With

    GetLatestFile strFolder

End Sub

Sub GetLatestFile(strFolder  As String)

Dim strFile    As String
Dim latestFile As String
Dim dtLast     As Date

'   assign variables
    strFolder = strFolder & "\"
    strFile = Dir(strFolder & "\*.xls*", vbNormal) ' Excel Files
'   strFile = Dir(strFolder & "\*.csv", vbNormal)  ' CSV Files
'   strFile = Dir(strFolder & "\*.*", vbNormal)    ' Any File

'   loop through files to find latest modified date
    Do While strFile <> ""
        If FileDateTime(strFolder & strFile) > dtLast Then
            dtLast = FileDateTime(strFolder & strFile)
            latestFile = strFolder & strFile
        End If
        strFile = Dir
    Loop

    MsgBox latestFile

End Sub

我刚刚测试了两段代码,它们对我有用。如果你不能让他们工作,请告诉我。

于 2013-12-08T05:42:23.110 回答