I am attemping to export the workbook/sheet I am working on to a Sharepoint.
Regardless of the object I am trying to save (book/sheet), I am getting an error. “Method ‘SaveAs of object ‘_Workbook’ failed “
The SharePoint is outside my organization, but I have been granted access rights to upload and create files there.
Have any of You encountered such difficulties?
Sub ExportSheetToSharePoint()
Dim wsExport As Worksheet
Dim wsLogowanie As Worksheet
Dim newBook As Workbook
Dim fileName As String
Dim sharePointURL As String
Dim sharePointFilePath As String
Dim part1 As String
Dim part2 As String
Dim part3 As String
Dim wasVeryHidden As Boolean
Dim ws As Worksheet
'Sharepoin link in 4 parts
part1 = url1
part2 = url2
part3 = url3
part4 = url4
sharePointURL = part1 & part2 & part3 & part4
On Error Resume Next
Set wsExport = ThisWorkbook.Sheets("export")
Set wsLogowanie = ThisWorkbook.Sheets("Logowanie")
On Error GoTo 0
wasVeryHidden = (wsExport.Visible = xlSheetVeryHidden)
If wasVeryHidden Then
wsExport.Visible = xlSheetVisible
End If
' nazwa pliku Logowanie!E10
fileName = "test" + ".xsls"
Set newBook = Workbooks.Add
wsExport.Copy Before:=newBook.Sheets(1)
If wasVeryHidden Then
wsExport.Visible = xlSheetVeryHidden
End If
newBook.Sheets("export").Visible = xlSheetVisible
Application.DisplayAlerts = False
For Each ws In newBook.Worksheets
If ws.Name <> "export" Then
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
sharePointFilePath = sharePointURL & fileName
** newBook.SaveAs sharePointFilePath, FileFormat:=xlOpenXMLWorkbook**
On Error GoTo 0
newBook.Close SaveChanges:=False
MsgBox "Export complete: " & sharePointFilePath
' Cleanup
Set newBook = Nothing
Set wsExport = Nothing
Set wsLogowanie = Nothing
End Sub
ErrorDestroyer666 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.