我尝试组合了几个不同的脚本,这些脚本工作正常,直到我更改strPath
的路径。我将其更改为其他任何内容的那一刻我收到错误消息
无效的过程调用或参数。
该脚本用于查找任何目录(包括子文件夹)中的最新文件,并将文件复制并粘贴到文件夹中
Dim strPath, oFSO, oFile, oFolder, dteDate, strName, N
strPath = "C:\Users\parjo16\Documents\Archived"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
For Each oFile In oFolder.Files
If oFile.DateLastModified > dteDate Then
dteDate = oFile.DateLastModified
strName = oFile.Name
End If
N = N + 1
Next 'oFile
Call FindTheSubFolderFiles(oFolder, N, dteDate, strNme)
Const strfolder = "C:\SalaryData\"
Const Overwrite = True
Dim oFSOd
Set oFSOd = CreateObject("Scripting.FileSystemObject")
If Not oFSOd.FolderExists(strfolder) Then
oFSOd.CreateFolder strfolder
End If
oFSOd.CopyFile strNme, strfolder & "salaries.xlsx", Overwrite
Set oFSOd = Nothing
Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
Function FindTheSubFolderFiles(ByRef oParentFolder, ByRef lngR, ByRef dteDte, ByRef strNme)
Dim oSubFolder
Dim oFile
For Each oSubFolder In oParentFolder.SubFolders
For Each oFile In oSubFolder.Files
If oFile.DateLastModified > dteDte Then
dteDte = oFile.DateLastModified
strNme = oFile.Path
End If
lngR = lngR + 1
Next
FindTheSubFolderFiles oSubFolder, lngR, dteDte, strNme
Next 'oSubFolder
Set oSubFolder = Nothing
Set oFile = Nothing
End Function
答案 0 :(得分:0)
看起来脚本正在进行两次检查,一次针对主文件夹,一种针对子文件夹。如果结果是另一个,则它返回空白。
这似乎现在正在起作用:
Function FindTheSubFolderFiles(ByRef oParentFolder, ByRef lngR, ByRef dteDte, ByRef strNme)
Dim oSubFolder
Dim oFile
For Each oSubFolder In oParentFolder.SubFolders
For Each oFile In oSubFolder.Files
If oFile.DateLastModified > dteDte Then
dteDte = oFile.DateLastModified
strNme = oFile.path
End If
lngR = lngR + 1
Next
FindTheSubFolderFiles oSubFolder, lngR, dteDte, strNme
Next 'oSubFolder
Set oSubFolder = Nothing
Set oFile = Nothing
End Function
Dim strPath, oFSO, oFile, oFolder, dteDate, strName, N
strPath = "C:\Users\parjo16\Documents\Archived\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(strPath)
For Each oFile In oFolder.Files
If oFile.DateLastModified > dteDate Then
dteDate = oFile.DateLastModified
strName = oFile.Name
End If
N = N + 1
Next 'oFile
Call FindTheSubFolderFiles(oFolder, N, dteDate, strNme)
Const strfolder = "C:\SalaryData\"
Const Overwrite = True
Dim oFSOd
Set oFSOd = CreateObject("Scripting.FileSystemObject")
If Not oFSOd.FolderExists(strfolder) Then
oFSOd.CreateFolder strfolder
End If
On Error Resume Next
oFSOd.CopyFile strPath & strName, strfolder & "salaries.xlsx", Overwrite
oFSOd.CopyFile strNme, strfolder & "salaries.xlsx", Overwrite
Set oFSOd = Nothing
Set oFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing