1

所以我对VBA很陌生。

下面的代码在 2007 年可以正常工作,用于列出PDF files特定文件夹中的所有内容。但是,当我在 excel 2010 中尝试此代码时,它似乎不起作用(它会引发错误Set fold = fso.GetFolder(folderPath)

任何想法我做错了什么?

我确实检查了脚本运行时。我的代码如下:

Sub List_files()

Dim fso As FileSystemObject
Dim fold As Folder
Dim f As File
Dim folderPath As String
Dim i As Integer

folderPath = "S:\Academic Affairs\Academic Operations Reporting\CV's"
Set fso = New FileSystemObject
Set fold = fso.GetFolder(folderPath)

i = 2
For Each f In fold.Files
    If LCase(Right(f.Name, 3)) = "pdf" Then
        Range("A" & i).Value = f.Name
        i = i + 1
    End If
Next

End Sub
4

5 回答 5

2

这是我用于列出文件的过程:

Function GetFileList(pDirPath As String) As Variant
On Error GoTo GetFileList_err

    ' Local constants / variables
    Const cProcName = "GetFileList"
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim c As Double           ' upper bound for file name array
    Dim i As Double           ' iterator for file name array
    Dim vFileList() As String ' array for file names

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(pDirPath)
    c = objFolder.Files.Count
    i = 0

    ReDim vFileList(1 To c)  ' set bounds on file array now we know count

    'Loop through the Files collection
    For Each objFile In objFolder.Files
        'Debug.Print objFile.Name
        i = i + 1
        vFileList(i) = objFile.Name
    Next

    'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing

    GetFileList = vFileList

GetFileList_exit:
    Exit Function

GetFileList_err:
    Debug.Print "Error in ", cProcName, " Err no: ", Err.Number, vbCrLf, "Err Description: ", Err.Description
    Resume Next

End Function

Sub PrintFileList(pDirPath As String, _
                  Optional pPrintToSheet = False, _
                  Optional pStartCellAddr = "$A$1", _
                  Optional pCheckCondition = False, _
                  Optional pFileNameContains)
On Error GoTo PrintFileList_err

    ' Local constants / variables
    Const cProcName = "PrintFileList"
    Dim vFileList() As String ' array for file names
    Dim i As Integer          ' iterator for file name array
    Dim j As Integer          ' match counter
    Dim c As String

    vFileList = GetFileList(pDirPath)
    c = pStartCellAddr
    j = 0

    For i = LBound(vFileList) To UBound(vFileList)
        If pPrintToSheet Then
            If pCheckCondition Then
                ' if pFileNameContains not in filename go to next iteration of loop
                If InStr(1, vFileList(i), pFileNameContains, vbTextCompare) = 0 Then
                    GoTo EndLoop
                End If
            End If
            Range(c).Offset(j, 0).Value = vFileList(i)
            j = j + 1
        End If
        'Debug.Print vFileList(i)
        i = i + 1
EndLoop:
    Next

PrintFileList_exit:
    Exit Sub

PrintFileList_err:
    Debug.Print "Error in ", cProcName, vbCrLf, "Err no: ", Err.Number, _
                vbCrLf, "Err Description: ", Err.Description
    Resume Next

End Sub

该函数仅供内部使用,您调用该过程。这是一个示例调用(在这种情况下,使用 userprofile windows 环境变量作为路径而不是硬编码路径):

call PrintFileList(environ("userprofile"), True, "$A$1", True, ".pdf")
于 2013-08-05T22:21:13.860 回答
2

我认为您需要在 folderPath 变量上使用“\”...

folderPath = "S:\Academic Affairs\Academic Operations Reporting\CV's\"

如果那不能解决它,请发布您遇到的错误。

于 2013-08-05T16:46:00.347 回答
2

每当事情没有按照他们“应该”的那样工作时,从一个可以工作并从那里构建的最小方法开始是非常有成效的。试试这个在 Excel 2016 中有效的方法:

Option Explicit

Sub File_renaming2()
    Dim objFSO As FileSystemObject
    Dim mySource As Folder
    Dim myFolder As File

    Set objFSO = New FileSystemObject
    Set mySource = objFSO.GetFolder("S:\Academic Affairs\Academic Operations Reporting\CV's\")
    For Each myFolder In mySource.Files
        Debug.Print myFolder.Name
    Next myFolder
End Sub
于 2018-02-21T16:47:02.547 回答
1

用这个:

Set fso = New Scripting.FileSystemObject
于 2019-05-21T12:08:23.080 回答
1

不知道怎么解释:但是我们需要对对象类型进行完整的引用

CHANGE                            
    "Dim mySource As Folder "         
TO
    "Dim mySource As Scripting.Folder"    'OR "Dim mySource As object"     

为什么 ?在我的情况下,工作代码停止工作=>我添加了“microsoft Outlook对象库”=>它有一个“文件夹”类型=>所以没有什么对我有用

于 2020-02-05T19:13:56.623 回答