gpt4 book ai didi

excel - VBA 用字符串中的另一个对象修剪一个对象(FSO.Getfolder 相关)基本上最终得到文件的相对路径

转载 作者:行者123 更新时间:2023-12-02 13:48:11 29 4
gpt4 key购买 nike

我正在目录中构建文件索引。第 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

我发现改变了
单元格(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)

关于excel - VBA 用字符串中的另一个对象修剪一个对象(FSO.Getfolder 相关)基本上最终得到文件的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37183046/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com