我在 Excel 中创建了一个宏,用于将文件从一个文件夹移动到另一个文件夹。我在本地计算机上执行了此操作,并且没有考虑到该宏会对使用远程桌面访问同一 Excel 的其他用户产生影响。我意识到我使用的路径是针对 OneDrive 的,需要修改才能与 SharePoint 配合使用。我尝试了网上能找到的所有组合,最后请教了专家。
提前谢谢您(下面是我的代码)
Sub MoveFiles()
'Move previous days' files to folder "PreviousRateSheets"
Dim SourceFolderPath As String
Dim DestinationFolderPath As String
Dim FSO As Object
Dim SourceFolder As Object
Dim File As Object
Dim FileName As String
Application.ScreenUpdating = False
'ORIGINAL CODE - Worked on my local machine but not on other users' machines
'sourceFolderPath = "C:\Users\XXX\OneDrive - XXX, Inc\XXX\XXX\XXX\XXX\TodayRateSheet"
'destinationFolderPath = "C:\Users\XXX\OneDrive - XXX, Inc\XXX\XXX\XXX\XXX\PreviousRateSheets"
'SECOND ATTEMP USING "Environ("OneDrive")" - Didn't work on either my machine nor my RDP
'sourceFolderPath = Environ("OneDrive") & "\XXX\XXX\XXX\TodayRateSheet\"
'destinationFolderPath = Environ("OneDrive") & "\XXX\XXX\XXX\XXX\PreviousRateSheets"
'This Path structure works elsewhere in another script to save the requisite files to SharePoint
SourceFolderPath = "https://XXX.sharepoint.com/XXX/XXX/XXX/XXX/XXX/XXX/TodayRateSheet/"
DestinationFolderPath = "https://XXX.sharepoint.com/XXX/XXX/XXX/XXX/XXX/XXX/PreviousRateSheet/"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.Getfolder(SourceFolderPath)
For Each File In SourceFolder.Files
FileName = File.Name
sourceFilePath = File.Path
destinationFilePath = DestinationFolderPath & "/" & FileName
FSO.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath
Next
'Don't need set file to nothing because it is initialized in for each loop
'and after this loop is automatically set to Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
使用
Environ ("USERPROFILE")
代替 C:\Users\yourusername
所以代替:
sourceFolderPath = "C:\Users\XXX\OneDrive - XXX, Inc\XXX\XXX\XXX\XXX\TodayRateSheet"
destinationFolderPath = "C:\Users\XXX\OneDrive - XXX, Inc\XXX\XXX\XXX\XXX\PreviousRateSheets"
尝试:
sourceFolderPath = Environ("USERPROFILE") & "\OneDrive - XXX, Inc\XXX\XXX\XXX\XXX\TodayRateSheet"
destinationFolderPath = Environ ("USERPROFILE") & "\OneDrive - XXX, Inc\XXX\XXX\XXX\XXX\PreviousRateSheets"
请注意,用户确实可以为其 OneDrive 根文件夹选择不同的位置,并且他们可能会同步到不同文件夹级别的 SharePoint 网站 - 其中任何一个都需要对路径进行进一步更改。