I've had success using VBA in Excel to map a drive to extranet SharePoint to download files, however in deployment it works in one location but not another (different environments possible). I'm curious if anyone has any insight as to what user or system setting would cause this.
In the code below I try to map the drive to SharePoint, if it errors out the handler creates a new instance of excel and saves it to the SharePoint site. By nature this forces IE to open and prompt the user for their login details, once submitted it authenticates them and uploads the file. They are then able to map the drive to SharePoint. The problem I'm having with the one group is it will upload the file, however they do not stay authenticated to map the drive. Even weirder, the user is logged into the SharePoint site in IE while I'm stepping through this procedure.
Sub MapSharePoint()
Dim objNet as object
Dim strDriveLetter as String
Dim strSharePointDatabaseFolder as String
Set objNet = CreateObject("WScript.Network")
On Error GoTo AUTH_Connection:
strDriveLetter = <function to find open drive>
strSharePointDatabaseFolder = <SharePoint site>
objNet.MapNetworkDrive strDriveLetter, strSharePointDatabaseFolder
<do something with mapped drive>
Exit Sub
AUTH_Connection:
Dim xlApp As New Excel.Application
Dim xlDoc As Workbook
On Error GoTo ErrHandler:
Set xlApp = CreateObject("Excel.Application")
Set xlDoc = xlApp.Workbooks.Add
' Trying to upload the file below will force IE to open and prompt user for their Username and Password which will authenticate them
xlDoc.SaveAs FileName:="<SharePointSite>", FileFormat:=xlWorkbookNormal, AddToMru:=False
xlDoc.Close
xlApp.Quit
objNet.MapNetworkDrive strDriveLetter, strSharePointDatabaseFolder
Resume Next
ErrHandler:
MsgBox Err.Code, Err.Description
End Sub
UPDATE 1:
Using the code below the problem I'm running into is the SharePoint authentication. In the catch brackets I added the line of code below to pop a message window with the specific error text, and was getting 403: Forbidden. After download Fiddler I can see that the site is using an authentication cookie, which I've read WebClient does not support. I've been trying to capture the cookie and authenticate using it, so now I don't get the 403 error, but instead I'm downloading the HTML code from the web form login. I need to figure out how to send a login request, capture the auth cookie that comes back, and then use that when sending the DownloadFile request.
System.Windows.Forms.MessageBox.Show(ex.Message);