Mapping network drive...

D

Dave Peterson

One more:

Option Explicit
Sub testme()
Dim res As Variant
res = TryToMapIt("G", "\\myserver\mypath")
If res = True Then
MsgBox "it worked"
Else
MsgBox res
End If
End Sub
Function TryToMapIt(myDrive, NewPath) As Variant

Dim FixedIt As Variant
Dim FSO As Object
Dim WSHNetwork As Object

Set WSHNetwork = CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.driveexists(myDrive) Then
WSHNetwork.RemoveNetworkDrive myDrive, True, True
End If

FixedIt = False
On Error Resume Next
WSHNetwork.MapNetworkDrive myDrive, NewPath, True
If Err.Number = 0 Then
FixedIt = True
Else
If Err.Number <> 0 Then
FixedIt = Err.Description
End If
Err.Clear
End If

TryToMapIt = FixedIt

End Function
 
Top