VBA - 错误53:找不到文件,文件名中有非拉丁字符

时间:2018-05-15 02:06:36

标签: vba excel-vba excel

我有代码将所有级别的已定义文件夹及其子文件夹添加到集合中。当遇到丹麦语字符“ø”的文件时,它会在attrVal = GetAttr(path & currentPath)行的标题中提到错误而崩溃。有什么办法可以对包含非拉丁字母的文件和文件夹进行操作吗?

Option Explicit

Public OutCollection As New Collection
Public MainPath As String

Sub test()
    Set OutCollection = Nothing       
    MainPath = "C:\Users\Me\Desktop\test folder\"
    TraversePath MainPath
End Sub

Sub TraversePath(path As String)

    Dim currentPath As String
    Dim directory As Variant
    Dim dirCollection As New Collection
    Dim attrVal As Long

    If path = MainPath Then OutCollection.Add path

    currentPath = Dir(path, vbDirectory)

    'current directory
    Do Until currentPath = vbNullString
        If Left(currentPath, 1) <> "." Then
            attrVal = GetAttr(path & currentPath)
            Select Case attrVal
            Case 16, 48 'folder or folder marked for backup or removal
                dirCollection.Add currentPath
                OutCollection.Add path & currentPath
            End Select
        End If
        currentPath = Dir()
    Loop

    'subsequent directories
    For Each directory In dirCollection
        TraversePath path & directory & "\"
    Next directory

End Sub

1 个答案:

答案 0 :(得分:1)

您可以尝试FSO是否可以使用非拉丁字符:

Public Function GetFileInfo(ByVal sFile As String)
    On Error GoTo Error_Handler
    Dim fso                   As Object
    Dim f                     As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFile(sFile)

    Debug.Print f.Name               
    Debug.Print , "Size: " & f.Size  
    Debug.Print , "Created: " & f.DateCreated
    Debug.Print , "Modified: " & f.DateLastModified
    Debug.Print , "Accessed: " & f.DateLastAccessed
    Debug.Print , "Type: " & f.Type
    Debug.Print , "Attributes: " & f.Attributes

Error_Handler_Exit:
    On Error Resume Next
    Set f = Nothing
    Set fso = Nothing
    Exit Function

Error_Handler:
    MsgBox Err.Description, vbCritical, "Error " & Err.Number, Err.HelpFile, Err.HelpContext
    Resume Error_Handler_Exit
End Function

查看Attributes Property以查看返回.Attributes的值。