2

我正在目录中构建此文件索引。第 1 列包含文件夹名称,第 2 列包含文件名

我已经设法获得了实际的文件名并已经超链接了文件名。但是我在使用包含其子文件夹的相对路径列出第 1 列中的文件路径时遇到问题。

假设我有以下文件夹:“C:\users\ME\Documents” 在该文件夹内有许多子文件夹。

我想要实现的是一个列出该实际子文件夹路径的字符串。例子:

"C:\users\ME\Documents\Subfolder1\Subfolder2\CharlieSheen.pdf"
Column 1 (A5) = Subfolder1\Subfolder2\
Column 2 (B5) = CharlieSheen.pdf

正如我所说,我可以控制第 2 列。

我已经使用的脚本是

Private Function GetAllFiles(ByVal strpath As String, _
ByVal intRow As Integer, ByRef objFSO As Object) As Integer
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer`
i = intRow - ROW_FIRST + 1

Set objFolder = objFSO.Getfolder(strpath)
For Each objFile In objFolder.Files
'print file path
    Cells(i + ROW_FIRST - 1, 1) = objFolder.Name

i = i + 1
    Next objFile
    GetAllFiles = i + ROW_FIRST - 1
End Function

我发现改变
Cells(i + ROW_FIRST - 1, 1) = objFolder.Name

intoCells(i + ROW_FIRST - 1, 1) = objFSO.Getfolder(".")准确地返回了我想从第一个字符串中删除的内容!

所以我基本上想写一个脚本,上面写着:

Cells(i + ROW_FIRST - 1, 1) = objFolder.Name - objFSO.Getfolder(".")

但我需要帮助,因为该命令显然不起作用。

可能有一种完全不同的方法,但由于我的宏已经有很多代码,使用 Trim 或 Replace 或类似的方法是最简单的吗?

编辑: 我的脚本中还有一个名为“GetAllFolders”的函数。也许我可以以某种方式调用它来实现我想要的字符串?

Private Sub GetAllFolders(ByVal strFolder As String, _
    ByRef objFSO As Object, ByRef intRow As Integer)
Dim objFolder As Object
Dim objSubFolder As Object

'Get the folder object
Set objFolder = objFSO.GetFolder(strFolder)
'loops through each file in the directory and
'prints their names and path
For Each objSubFolder In objFolder.subfolders
    intRow = GetAllFiles(objSubFolder.Path, _
        intRow, objFSO)
    'recursive call to to itsself
    Call GetAllFolders(objSubFolder.Path, _
        objFSO, intRow)
Next objSubFolder
End Sub
4

1 回答 1

2

关于什么

Cells(i + ROW_FIRST - 1, 1) = Replace$(objFolder.Name, CStr(objFSO.Getfolder(".")), vbNullString)
于 2016-05-12T09:45:25.810 回答