Here is the code I used, somewhat simplified. It checks to see if there are already connections to the target network drive, removes them, connects under a different user name, moves a file, disconnects, and reconnects the original network drive configuration.
Dim objNetwork
Dim objDrivePath
Dim fl As Scripting.FileSystemObject
Dim fArchive As Object
Dim fWIP As Object
Dim [other variables]
strDriveLetter = "K:"
strRemotePath = [file location]
Set objNetwork = CreateObject("WScript.Network")
strUserName = [user name of "Service Account" logon credentials created by IT department]
strPassWord = [password for Service Account]
'---You cannot sign on to the same network drive under different logons at the same time, so you must disconnect any existing connections---
' Check for current connections, and disconnect if mapped to the target folder
Dim strCurrentDriveMap As String
Dim strCurrentDriveLetter As String
Set objDrivePath = CreateObject("WScript.Network")
Set oDrives = objDrivePath.EnumNetworkDrives
For i = 0 To oDrives.Count - 1
If oDrives.Item(i) Like "\\networkpath\foldername*" Then
strCurrentDriveMap = oDrives.Item(i)
strCurrentDriveLetter = oDrives.Item(i - 1)
objDrivePath.RemoveNetworkDrive strCurrentDriveLetter, True, False
'MsgBox oDrives.Item(i)
'MsgBox strCurrentDriveLetter & " = " & strCurrentDriveMap
Else
End If
Next
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, False, strUserName, strPassWord
Set fl = New Scripting.FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
' Set the folder name
foldername1 = [variable]
'Set the current directory and destination directory
varCurrentDirectory = [folder location]
varNewDirectory = [folder location]
'If the destination folder does not exist, then create it
If Dir(varNewDirectory, vbDirectory) = "" Then
MkDir varNewDirectory
End If
'Set the file name
filename1 = [variable]
varOldFileName = varCurrentDirectory & filename1 & ".doc"
varNewFileName = varNewDirectory & filename1 & ".doc"
objDrivePath.RemoveNetworkDrive "K:", True, True
objNetwork.MapNetworkDrive strCurrentDriveLetter, strCurrentDriveMap, True
fl.MoveFile varOldFileName, varNewFileName
Set dbs = Nothing
Set qd = Nothing
objDrivePath.RemoveNetworkDrive "K:", True, True
'Check to see if there is a network drive that needs to be remapped
If strCurrentDriveLetter = "" Then
Else
objNetwork.MapNetworkDrive strCurrentDriveLetter, strCurrentDriveMap, True
End If
Set objDrivePath = Nothing
Set objNetwork = Nothing