-1

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?

4
  • 1
    If you have a local sync of the library then you can create the folder in the local copy. mkdir can't create a SP folder directly. Commented Nov 4 at 18:58
  • The file is going to be used by other users who may or may not have a local sync Commented Nov 4 at 19:03
  • As I understand it SP folders are actually SQL Server tables so there is no way MkDir will come close. This question has an upvoted answer which may or may not work. stackoverflow.com/questions/53497462/… Commented Nov 4 at 20:47
  • What have you done to research this? SharePoint is not file system based -- SharePoint content (including "folders") are in a database. You'll need to use SharePoint API if you're wanting to create a folder in SharePoint. Commented Nov 4 at 21:10

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

This is not creating a folder in SharePoint. It's creating a folder in the file system. SharePoint is in a database, not in the file system. To create a folder in SharePoint, you must use SharePoint API. For example: learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/…
@devlincarnate - did you try it? I did, and it worked in my testing. If you tried it and it didn't work, did you get an error?
That code may very well create a folder in the file system. And it's possible to link a mapped drive to SharePoint, but that folder is not stored in SharePoint -- it's linked to it and it is not a SharePoint folder -- it's a file system folder. But the question the OP asked, while missing a lot of details, asks how to create a new folder in SharePoint. TL;DR: your code doesn't answer the question that was asked so it doesn't matter whether or not it works.
The OP just wants to be able to create a folder in SP. If they were able to count on a synched folder and created the new folder there, it would by synched to the SP location, also meeting their needs, and that would also be unacceptable to you? I don't understand your objections.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.