VBA用字符串中的另一个对象修剪一个对象(与FSO.Getfolder相关)基本上以文件的相对路径结束



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

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

假设我有以下文件夹:"C:\users\ME\Documents"在那个文件夹里有许多子文件夹。

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

"C:usersMEDocumentsSubfolder1Subfolder2CharlieSheen.pdf"
Column 1 (A5) = Subfolder1Subfolder2
Column 2 (B5) = CharlieSheen.pdf

正如我所说,我可以控制第二栏。

我已经使用的脚本是

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

into Cells(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

怎么样

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

最新更新