using a macro to map a shared drive

  • Thread starter Popeye the powerman
  • Start date
P

Popeye the powerman

Gents,
Is there a way to write a macro that when its run it will map a network drive.
it needs to also include the reconnect at logon.
This will save me having to tell people how to do it all the time

regards

Popeye
 
D

Dave Peterson

Always using the same drive letter and the same path???

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
 
P

Popeye the powerman

Dave,
I assume I replace ("G", "\\myserver\mypath") with my drive letter and server
if thats the case it doesn't appear to work and comes back with "the
specified device name is invalid".
Is there another bit of the code I should be altering.
Also yes it will always be the same letter
 
D

Dave Peterson

Try connecting (and disconnecting manually).

Then share the details of what worked manually.

Then share how you changed the code.

The code worked for me in the past -- but I don't have access to a network, so I
won't be able to do any real testing.

But if you post what you tried, maybe some reader will see a typo...
 
P

Popeye the powerman

Dave,

Found this on the scripting forum & does what I wanted

Share = "\\Drive\Group"
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "Z:", Share

Thanks for your assistance
Popeye
 
D

Dave Peterson

I don't see how this would work, but the other not...

But glad you got something working.
Dave,

Found this on the scripting forum & does what I wanted

Share = "\\Drive\Group"
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "Z:", Share

Thanks for your assistance
Popeye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top