brl8
asked on
VBA excel - save to a UNC path that requires a username and password
Hi All,
I have a macro that saves two spreadsheets to a UNC path (the path is to a sharepoint list) which, in order to open in windows explorer, you need to provide credentials. If I have the directory open in windows explorer (already having provided my credentials) then the following line of code works. If I don't already have the directory open, the code fails with the error message" "Run-time error '1004': Incorrect function."
My question is: how do I adjust my saveAs code to pass credentials so that I can save to this UNC path without user intervention?
I've also tried the following lines of code. This one works and pulls up the Windows explorer window:
This one does not work - there is no error but nothing happens:
So, I'm pretty sure this is due to the fact that a username and password are required. Does anyone know how to either prompt for a username and password or somehow pass them in using ShellExecute?
Any suggestions will be appreciated!
I have a macro that saves two spreadsheets to a UNC path (the path is to a sharepoint list) which, in order to open in windows explorer, you need to provide credentials. If I have the directory open in windows explorer (already having provided my credentials) then the following line of code works. If I don't already have the directory open, the code fails with the error message" "Run-time error '1004': Incorrect function."
wb.SaveAs Filename:=outputfile, FileFormat:=51
My question is: how do I adjust my saveAs code to pass credentials so that I can save to this UNC path without user intervention?
I've also tried the following lines of code. This one works and pulls up the Windows explorer window:
ShellExecute 0, "open", "C:\Documents and Settings", 0, 0, 1
This one does not work - there is no error but nothing happens:
ShellExecute 0, "open", "\\MyServer\MyLibrary\Staff Directory", 0, 0, 1
So, I'm pretty sure this is due to the fact that a username and password are required. Does anyone know how to either prompt for a username and password or somehow pass them in using ShellExecute?
Any suggestions will be appreciated!
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks! That thread pointed me in the right direction. Here is what I ended up doing:
Set oWshNetwork = CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
'map a drive to the sharepoint server
On Error Resume Next
oWshNetwork.MapNetworkDrive "M:", strPath, "False", strUser, strPwd
If Err.Number <> 0 Then
MsgBox "Error accessing \\mySharePointSite\myLibrary\Staff Directory. Please verify that you are on the network."
SaveNewDir = 1
Exit Function
End If
'copy file to the newly mapped M:\ drive.
If fso.FileExists(outputFile) Then
fso.CopyFile outputFile, "M:\"
Else
MsgBox outputFile & " file not found."
End If
oWshNetwork.RemoveNetworkDrive "M:"
ASKER
Thanks!!!
brl8