I am trying to create a folder at https:\\myorg.sharepoint.com\sites\CASH ORDER\Testing using...
MkDir "//myorg.sharepoint.com/sites/CASH ORDER/Testing"
...but I keep getting a run-time error 76. How do I create this SharePoint folder?
I found the code below (with a few edits) at: https://learn.microsoft.com/en-us/answers/questions/5267629/creating-folders-in-sharepoint-site-with-vba?page=2#answers
Seems to work for me in light testing.
For general use you might want to make sure you use a drive letter which is not already in use by a user.
Option Explicit
Sub CreateFolderInSharePointAndUnmount()
'This is the URL to the SP library
Const SP_LIB As String = "https://contoso.sharepoint.com/sites/ABC/TestLibrary"
'Drive letter to use for temporary mount
Const DRIVE_LETTER As String = "Z:"
Dim network As Object, folderPath As String, folderName As String
Dim fs As Object
Set network = CreateObject("WScript.Network")
' Map the SharePoint library to a network drive letter
On Error Resume Next
network.MapNetworkDrive DRIVE_LETTER, SP_LIB
If Err.Number <> 0 Then
MsgBox "Failed to map network drive. Error: " & Err.Description, vbCritical
GoTo UnmapDrive
End If
On Error GoTo 0
folderPath = DRIVE_LETTER & "\Test2" ' Specify the folder path within the mapped drive
If Dir(folderPath, vbDirectory) <> "" Then ' Check if the folder already exists
MsgBox "Folder already exists.", vbExclamation
GoTo UnmapDrive
End If
' Create the folder
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateFolder folderPath
' Check if folder creation was successful
If Dir(folderPath, vbDirectory) <> "" Then
MsgBox "Folder created successfully.", vbInformation
Else
MsgBox "Failed to create folder.", vbCritical
GoTo UnmapDrive
End If
UnmapDrive:
network.RemoveNetworkDrive DRIVE_LETTER, True, True ' Unmap the network drive
End Sub
mkdircan't create a SP folder directly.