VBA – 无法创建文件夹 – fso.CreateFolder

huangapple go评论58阅读模式
英文:

VBA - unable to create folder - fso.CreateFolder

问题

以下是您要翻译的代码部分:

Sub KonwertujPliki()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim sourcePath As String
    Dim fileName As String
    Dim savePath As String
    Dim newFileName As String
    Dim wb As Workbook
    Dim fso As Object
    
   
    Set ws = ThisWorkbook.Worksheets("Lista") 
    
    Set fso = CreateObject("Scripting.FileSystemObject")
        
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
   
    For i = 2 To lastRow
    
        sourcePath = ws.Cells(i, "A").Value
                
        fileName = ws.Cells(i, "B").Value
                
        savePath = ws.Cells(i, "I").Value
               
        If fso.FileExists(sourcePath & "\" & fileName) Then
            
            newFileName = Left(fileName, Len(fileName) - 4) & ".xlsm"
            
            If Not fso.FolderExists(savePath) Then
                fso.CreateFolder (savePath)
            End If
          
            Set wb = Workbooks.Open(sourcePath & "\" & fileName)
            
            wb.SaveAs savePath & "\" & newFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            
            wb.Close SaveChanges:=True
        End If
    Next i
    
    MsgBox "Files copied."
End Sub

希望对您有所帮助。如果您有其他问题,请随时提出。

英文:

I need to open all files from folder and its subfolders and save them with same name but different file extension and different location.

As I do not have the knowledge how to do it in one macro I decided to divide it to 2 steps.

  1. Create list of files and then use formula to generate output file save location
  2. Loop through the list, open and then save as in location given by the formula

So far I have macro that creates list of all files in desired locations and place it in the excel table, then in column F I added formula that gives name for the file save location.

In column A I have folder path for the source file (eg. D:\RDKU\RDKU\2022\02)

Column B has the file full name including extension (eg. RDKU_01-02-2023_OR_ORLP_0115_20230202143527_RORT_RDKU_0000305583.xls )

Column I contains save folder (formula generated) destination

I got stuck on fso.CreateFolder (savePath).

Could you please review and advise ?

Sub KonwertujPliki()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim sourcePath As String
    Dim fileName As String
    Dim savePath As String
    Dim newFileName As String
    Dim wb As Workbook
    Dim fso As Object
    
   
    Set ws = ThisWorkbook.Worksheets("Lista") 
    
    Set fso = CreateObject("Scripting.FileSystemObject")
        
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
   
    For i = 2 To lastRow
    
        sourcePath = ws.Cells(i, "A").Value
                
        fileName = ws.Cells(i, "B").Value
                
        savePath = ws.Cells(i, "I").Value
               
        If fso.FileExists(sourcePath & "\" & fileName) Then
            
            newFileName = Left(fileName, Len(fileName) - 4) & ".xlsm"
            
            If Not fso.FolderExists(savePath) Then
                fso.CreateFolder (savePath)
            End If
          
            Set wb = Workbooks.Open(sourcePath & "\" & fileName)
            
            wb.SaveAs savePath & "\" & newFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            
            wb.Close SaveChanges:=True
        End If
    Next i
    
    MsgBox "Files copied."
End Sub

I tried different parts of code from various forums , however it does not want to work as on efull code.
I believe this is the closes to truth version.
Folder path name is not longer that 255 characters

Much appreciate your help

Kind regards

Piotr

答案1

得分: 3

(a) 缺少权限 - 如果您没有权限创建路径,那就没有办法了。除了请求权限,您无能为力。

(b) 无效名称 - 文件夹(或文件)名称中有一些不允许的字符。同样,您没有办法,需要更改名称。

(c) 您想要创建一个丢失的文件夹 D:\RDKU\RDKU\2022\02,但其中一个父文件夹已经丢失了。只有当文件夹 2022 已经存在时,fso.CreateFolder 才能创建文件夹 02。您可以通过先创建父文件夹来解决此问题。以下代码尝试这样做:

Sub createFolder(folder As String)
    ' 适用于带有驱动器号的 Windows 路径和 UNC 路径
    ' c:\folder\subfolder\subsubfolder
    ' \\server\share\folder\subfolder\subsubfolder
    
    Dim fso As Object, path As String, subfolder() As String
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' 分割成子文件夹名称
    subfolder = Split(folder, "\")
    Dim startIndex As Long, i As Long
    If Left(folder, 2) = "\\" Then
        ' UNC 路径
        ' subfolder(0) 和 subfolder(1) 为空
        ' 我们不能创建服务器和共享名称,只能创建以下文件夹
        path = "\\" & subfolder(2) & "\" & subfolder(3)
        startIndex = 4
    Else
        path = subfolder(0)
        startIndex = 1
    End If
    
    For i = startIndex To UBound(subfolder)
        path = path & "\" & subfolder(i)
        If Not fso.FolderExists(path) Then
            fso.CreateFolder path
        End If
    Next
End Sub
英文:

I can think of 3 possible reasons why you can't create the path:

(a) Missing priviledges - if you are not allowed to create the path, you are out of luck. Nothing you can do except asking for permissing.
(b) Invalid names - there are some characters that are not allowed in a folder (or file) name. Again, you are out of luck and need to change the name.
(c) You want to create a missing folder D:\RDKU\RDKU\2022\02, but already one of the parent folders is missing. fso.CreateFolder can only create the folder 02 if the folder 2022 already exists. You can address this by creating the parent folder(s) first. The following code is an attempt to do so:

Sub createFolder(folder As String)
    ' Works on Windows pathes with drive letter and on UNC pathes
    ' c:\folder\subfolder\subsubfolder
    ' \\server\share\folder\subfolder\subsubfolder
    
    Dim fso As object, path As String, subfolder() As String
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Split into subfolder names
    subfolder = Split(folder, "\")
    Dim startIndex As Long, i As Long
    If Left(folder, 2) = "\\" Then
        ' UNC path
        ' subfolder(0) and subfolder(1) are empty
        ' We cannot create server and share name, only folders below
        path = "\\" & subfolder(2) & "\" & subfolder(3)
        startIndex = 4
    Else
        path = subfolder(0)
        startIndex = 1
    End If
    
    For i = startIndex To UBound(subfolder)
        path = path & "\" & subfolder(i)
        If Not fso.FolderExists(path) Then
            fso.createFolder path
        End If
    Next
End Sub

huangapple
  • 本文由 发表于 2023年6月15日 17:36:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定