I’m getting an error with this code when trying to save to Sharepoint. Code is longer, but including relevant pieces.
Dim fso As Object
Dim template As Workbook
Dim TempPath As String 'path is set with folder picker
Set fso = CreateObject("Scripting.FileSystemObject")
currentname = fso.GetBaseName(ThisWorkbook.Name)
Set template = Workbooks.Open(TempPath)
DoEvents
If InStr(ThisWorkbook.Path, "/") > 0 Then 'check if it's saving to Sharepoint
urlpath = Replace(ThisWorkbook.Path, " ", "%20")
urlname = Replace(currentname, " ", "%20")
Savename = urlpath & "/" & urlname & "_New" & ".xlsm"
Else
Savename = ThisWorkbook.Path & "" & currentname & "_New" & ".xlsm"
End If
currentformat = template.FileFormat
template.SaveAs Filename:=Savename, FileFormat:=currentformat
The code works when ‘ThisWorkbook’ is in the system file library, but errors when file is in Sharepoint. I recorded a ‘Save’ macro and the system shows filename as (example) https:// “companyname.sharepoint.com/sites/team_data/Shared%20Documents/folder%20location”
If I assign Savename with that address specifically, it works. So the problem seems to be with using the concatenation of the workbook path and name. If I display the concatenated name in a message box, it appears to be the exact same as the direct path. I can’t figure out what the issue is with the concatenation. The workbook.path for sharepoint returns with spaces instead of %20. I added the ‘replace’ code to add the %20 to match how the save macro showed it.