我正在构建目录中的这个文件索引。 第1列包含文件夹名称,第2列包含文件名
我已经设法获取实际文件名并已经超链接文件名。 但是我在使用包含其子文件夹的相对路径列出第1列中文件的路径时遇到了问题。
说我有以下文件夹: “C:\用户\ ME \文件” 在该文件夹中有许多子文件夹。
我想要实现的是一个字符串,列出该实际子文件夹的路径。 例如:
"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
进入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
答案 0 :(得分:2)
怎么样?
Cells(i + ROW_FIRST - 1, 1) = Replace$(objFolder.Name, CStr(objFSO.Getfolder(".")), vbNullString)