AOB
Registered User.
- Local time
- Today, 01:37
- Joined
- Sep 26, 2012
- Messages
- 617
Hi guys,
Starting a new thread for an old question (original thread here) which I'm close to solving but have one last sticking point.
Original thread gives the background but basically I'm trying to download a file from a Sharepoint site via VBA. Had tried using FSO.CopyFile and the URLDownloadToFile API but neither were able to retrieve the file consistently - I found I had to keep manually putting the URL into Windows Explorer to make the connection to the Sharepoint directory first before the code would work.
Anyway - my workaround was to temporarily map the URL to a vacant drive letter on the local machine, then copy the file over, then drop the mapped drive again. A bit clunky but doesn't incorporate much of a delay and this download only has to happen once per day so a few seconds isn't going to kill anyone.
The good news is, the file download / copy now works every time (hurray! ) The bad news is that removing the temporarily mapped drive after the copy has taken place, doesn't (boooh! ) and I can't figure out why.
Here are the functions I use to map / unmap the drive :
I have separate functions to check existing drive mappings on the local machine and thus determine an appropriate vacant letter to use for the temporary mapping - they work fine.
Unfortunately the UnMapDrive function returns False (even though I switch the active drive to C: and force the connection to be cancelled with the fForce flag) So the mapping always remains on the users profile.
Any ideas how I can get around this? I don't want to permanently map drives on the users' profiles, just briefly for the purposes of this daily file download.
Thanks
Al
Starting a new thread for an old question (original thread here) which I'm close to solving but have one last sticking point.
Original thread gives the background but basically I'm trying to download a file from a Sharepoint site via VBA. Had tried using FSO.CopyFile and the URLDownloadToFile API but neither were able to retrieve the file consistently - I found I had to keep manually putting the URL into Windows Explorer to make the connection to the Sharepoint directory first before the code would work.
Anyway - my workaround was to temporarily map the URL to a vacant drive letter on the local machine, then copy the file over, then drop the mapped drive again. A bit clunky but doesn't incorporate much of a delay and this download only has to happen once per day so a few seconds isn't going to kill anyone.
The good news is, the file download / copy now works every time (hurray! ) The bad news is that removing the temporarily mapped drive after the copy has taken place, doesn't (boooh! ) and I can't figure out why.
Here are the functions I use to map / unmap the drive :
Code:
Option Compare Database
Option Explicit
Private Const RESOURCETYPE_ANY = &H0&
Private Const CONNECT_UPDATE_PROFILE = &H1&
Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
Private Type NETCONNECT
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" (lpNetResource As NETCONNECT, _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" _
Alias "WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, _
ByVal fForce As Long) As Long
Public Function MapDrive(DriveLetter As String, DrivePath As String, Optional UserName As String, Optional Password As String) As Boolean
Dim NetR As NETCONNECT
With NetR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpLocalName = DriveLetter & ":"
.lpRemoteName = DrivePath
End With
MapDrive = (WNetAddConnection2(NetR, UserName, Password, CONNECT_UPDATE_PROFILE) = 0)
End Function
Public Function UnMapDrive(DriveLetter As String) As Boolean
Dim NetR As NETCONNECT
With NetR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpLocalName = DriveLetter & ":"
.lpRemoteName = ""
End With
ChDrive ("C") ' Ensure that the drive letter to be dropped is not active
UnMapDrive = (WNetCancelConnection2(DriveLetter, CONNECT_UPDATE_PROFILE, True) = 0)
End Function
I have separate functions to check existing drive mappings on the local machine and thus determine an appropriate vacant letter to use for the temporary mapping - they work fine.
Unfortunately the UnMapDrive function returns False (even though I switch the active drive to C: and force the connection to be cancelled with the fForce flag) So the mapping always remains on the users profile.
Any ideas how I can get around this? I don't want to permanently map drives on the users' profiles, just briefly for the purposes of this daily file download.
Thanks
Al