简体   繁体   中英

VBA for moving file from one Sharepoint folder to another in Excel

I want to write (or find) some VBA code that basically moves an XL sheet from one Sharepoint folder to another Sharepoint folder when someone clicks on the macro button.

The only solution that I found until now is that I need to map the Sharepoint link into local folder and then use .FSO code but this isn't working for me since there is no administrator access to do that for security reasons.

The only code which I found out until now is below

Function ConvertPath(path) As String
  ConvertPath = Replace(path, " ", "%20")
  ConvertPath = Replace(ConvertPath, "/", "\")
  ConvertPath = Replace(ConvertPath, "http:", "")
End Function
Private Sub Approve_Click()
  Dim sDocPath As String
  Dim sDocPathConv As String
  Dim sFileName As String
  Dim sTargetPath As String
  Dim sSourcePath As String
  Dim fso As FileSystemObject
  Set fso = New FileSystemObject ' CreateObject("Scripting.FileSystemObject")
  sDocPath = ThisWorkbook.path
  sFileName = "WorkBook.xlsx"

  sDocPathConv = ConvertPath(sDocPath)

  sSourcePath = sDocPathConv & "https://xxxx.sharepoint.com/sites/xxxxxx/" & sFileName
  Debug.Print "Source: " & sSourcePath     
  sTargetPath = sDocPathConv & "https://xxxx.sharepoint.com/sites/xxxxxxx/" & sFileName
  Debug.Print "Target: " & sTargetPath
  fso.CopyFile sSourcePath, sTargetPath, True
End Sub

And even this code is not working as it always gives an error which says "Path not found". I am not at all familiar with VBA programming.

Try syncing the SharePoint to OneDrive in Windows Explorer. Then code below should work.

Will also need Reference: Microsoft Scripting Runtime

Public Function strDisplayID() As String
    strDisplayID = Environ("USERNAME")
End Function

Private Sub CommandButton_Click()

    Dim fso As Object
    Dim fileLocation As String

    Set fso = CreateObject("Scripting.FileSystemObject")

    fileLocation = "C:\Users\" & strDisplayID & "\YourPath\YourFile".pdf"
    fso.MoveFile fileLocation

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM