Create SharePoint document folder using VBA in Excel

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
938
Office Version
  1. 365
Platform
  1. Windows
I am not familiar with SharePoint but my business has decided to move all records to SharePoint, so I need to be able to modify current Excel Macros to create folders in SharePoint instead of a network drive.
Does anyone know how I can modify the below code to achieve this? My below modifications do not seem to work.

Thank you to anyone who can help.

VBA Code:
    sFolder = Worksheets("List").Range("C2").Value & Range("C1").Value
  If Dir(sFolder, vbDirectory) = "" Then
    MkDir sFolder
  End If
  Sheets("Quality Log").Select
  Calculate
  ActiveSheet.Hyperlinks.Add anchor:=Selection, Address:=sFolder

Book1
BC
1Last Quality Record Created:Q4170
2Last Quality Record Directory:https://group.sharepoint.com/sites/StorageTestGroup1/Shared%20Documents/Marcos%20Sheet/Quality%20Records/
3Directory of All Quality Records:https://group.sharepoint.com/sites/StorageTestGroup1/Shared%20Documents/Marcos%20Sheet/Quality%20Records/
4
5
6Directory of OPS Formshttps://group.sharepoint.com/sites/StorageTestGroup1/Shared%20Documents/Marcos%20Sheet/
7https://group.sharepoint.com/sites/StorageTestGroup1/Shared%20Documents/Marcos%20Sheet/OPS%20097%20Rev%206%20Non%20Conformance%20Report.xlsx
8Customer Concern OPS Form:OPS 041 Customer Concerns Rev 7
9NCR OPS Form:OPS%20097%20Rev%206%20Non%20Conformance%20Report
10CAPA OPS FormsOPS 050 Corrective Action Request Rev 8
List


1742847060536.png

1742847086934.png
 
Found it online, see below. Hopes this helps someone out there:

VBA Code:
Sub CreateFolderInSharePointAndUnmount()



    Dim network As Object

    Dim folderPath As String

    Dim folderName As String

    Dim driveLetter As String

    Dim fs As Object

   

    ' Create instance of WScript.Network object

    Set network = CreateObject("WScript.Network")

   

    ' Specify the SharePoint site URL

    Dim sharepointURL As String

    sharepointURL = "https://your_sharepoint_site_url_here/sites/your_site_name_here/library_name"

   

    ' Specify the drive letter to map

    driveLetter = "Z:" ' Change this to your preferred drive letter

   

    ' Map the SharePoint document library to a network drive

    On Error Resume Next

    network.MapNetworkDrive driveLetter, sharepointURL

    If Err.Number <> 0 Then

        MsgBox "Failed to map network drive. Error: " & Err.Description, vbCritical

        GoTo UnmapDrive

    End If

    On Error GoTo 0

   

    ' Specify the folder path within the mapped drive

    folderPath = driveLetter & "\Test"

   

    ' Check if the folder already exists

    If Dir(folderPath, vbDirectory) <> "" Then

        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:

    ' Unmap the network drive

    network.RemoveNetworkDrive driveLetter, True, True

   

    ' Clean up objects

    Set network = Nothing

    Set fs = Nothing

End Sub
 
Upvote 0
Solution

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top