【问题标题】:How to list files in VBA with long paths? [duplicate]如何在 VBA 中列出具有长路径的文件? [复制]
【发布时间】:2020-12-15 17:56:23
【问题描述】:

我正在使用 FileSystemObject 从文件夹中的文件循环,但我发现在某些情况下我丢失了文件。

我有一个包含 20 个文件的文件夹,在尝试列出它们时我只看到 14 个。路径很长,所以它似乎跳过了长度超过 250 个字符的文件。

Sub CountFilesInFolder()
   
    Dim xFileSystemObject As Object
    Dim xFolder As Object
    Dim xFile As Object
    Dim countx As Long
    Dim xFolderName As String
    Dim rowIndex As Long
    
    xFolderName = "\\server\[long folder name]"
     
    Set xFileSystemObject = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFileSystemObject.GetFolder(xFolderName)
    
    For Each xFile In xFolder.Files
        rowIndex = rowIndex + 1
        Application.ActiveSheet.Cells(rowIndex, 1).Formula = xFile.Name
    Next
    
End Sub

还有其他方法可以使用 vba 提取文件列表(以及文件中的信息)吗?

我从另一个问题here 尝试了这两种方法,但我遇到了同样的问题

【问题讨论】:

标签: excel vba


【解决方案1】:
'use short name instead and check if all your files are listed

xFile.ShortName

【讨论】:

  • 请不要代码只回答
猜你喜欢
  • 1970-01-01
  • 2014-07-25
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
相关资源
最近更新 更多