Results 1 to 1 of 1

Thread: How to login to SharePoint using VBA

  1. #1
    VBAX Newbie
    Jun 2022

    How to login to SharePoint using VBA

    I am creating an access program that needs to access the Company SharePoint site. If the user has previously logged in using their Microsoft credentials, the code below maps an available drive letter to the SharePoint folder. However I would like to embed a specific user name and password in my code to access the SharePoint site. I was able to do this thru Internet Explorer, but Microsoft shut IE down on June 22nd and I’m not longer able to use the workaround.
    I’ve searched for 2 days and have not come up with any examples of code that allows me to login to SharePoint with a user name and password set in the code (Not necessarily the user name and password of the user). Any help would be greatly appreciated.

    Function fDownloadUpdaterFile(SharepointAddress As String, sLocalPath As String, sUpdateFileName As String) As Boolean
    Dim LocalAddress As String
        Dim sResult As String
        Dim sMappedDrive As String
    Dim objFolder As Object
        Dim objNet As Object
        Dim objFile As Object
        Dim fs As Object
    Dim bResult As Boolean
    'Create a mapped drive the the sharepoint folder
        sMappedDrive = fNextAvaliableDrive & ":"
        Debug.Print "Using mapped drive  " & sMappedDrive
    '20220616 - AMD Need these 3 lines to authenticate Sparepoint If they have authenticated from this computer in the past.
        Dim objFd As FileDialog
        Set objFd = Application.FileDialog(msoFileDialogFolderPicker)
        objFd.InitialFileName = "https://[Company Name]"
    Set objNet = CreateObject("WScript.Network")
        Set fs = CreateObject("Scripting.FileSystemObject")
        Debug.Print "SP Folder to map to:" & SharepointAddress
        objNet.MapNetworkDrive sMappedDrive, SharepointAddress
    Set objFolder = fs.GetFolder(sMappedDrive)
    'Check if the file you want to download is there
        If Dir(sMappedDrive & "/" & sUpdateFileName) = "" Then
            fLog "Update Failed:  Could not find " & sUpdateFileName & " on sharepoint."
            fDownloadUpdaterFile = False
            GoTo fDownloadUpdaterFile_Exit
        End If
    'Download the file
        Debug.Print "Copying " & sMappedDrive & "/" & sUpdateFileName & " to " & fGetFrontEndPath & sUpdateFileName
        bResult = fCopyFile(sMappedDrive & "/" & sUpdateFileName, fGetFrontEndPath & sUpdateFileName)
    If bResult Then
            fDownloadUpdaterFile = True
            fDownloadUpdaterFile = False
        End If
    On Error Resume Next
        objNet.RemoveNetworkDrive sMappedDrive, True, True
        Set objNet = Nothing
        Set fs = Nothing
        Exit Function
        If Err.Number = -2147023652 Then
            MsgBox "You need to relog into sharepoint."
            MsgBox Err.Number & ":" & Err.Description
            Debug.Print Err.Number & ":" & Err.Description
        End If
    fDownloadUpdaterFile = False
        GoTo fDownloadUpdaterFile_Exit
    End Function
    Last edited by Aussiebear; 06-27-2022 at 01:35 PM. Reason: Added code tags to supplied code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts