Map drives via VBA

joefrench

Active Member
Joined
Oct 4, 2006
Messages
357
At work, a script runs as we log on to the network that deletes and re-maps our network drives for us (which really stinks when you want to add a new mapped drive). Sometimes this script only removes the drives and doesn't re-map them.

I'm now considering making a project via vba that will map my drives for me if the script fails to do so. Is this at all possible? To have vba map drives?

Thanks in advance.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
To have VBA map drives:

Code:
Dim oNetwork As Object, sDrive As String, sPath As String

Set oNetwork = CreateObject("WScript.Network")
sDrive = "G:"
sPath = "\\server\folder"
oNetwork.MapNetworkDrive sDrive, sPath
 
Upvote 0
we have a similar problem here when user connect via VPN from outside, I tackled it as follows and created the following VBA which takes a snap shot of the current network drives and creates a batch file on the user desktop which can be double clicked on

Private Sub MapMyDrives_Click()
'****
' Function to create a batch file to write the users current network drives to.
' this can be used by the user to create the file on their desktop so when they
' login using VPN the drives can be mapped in a faster way than a full login
'
' Author Jim Ward
' Creation 10th April 2008
'
'****

'****
' Declare what we need, general
'****

Dim fs, d, dc, l, n, s

'****
' Declare what we need to get the Desktop location
'****

Dim oWSS As Object
Const szlocation As String = "Desktop"
Dim szDesktopPath As String

'****
' get the desktop location
'****

Set oWSS = CreateObject("WScript.Shell")
szDesktopPath = oWSS.SpecialFolders(szlocation)

'****
' get the list of drives
'****

Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
s = ""

'****
' Process each one, ignoring local drives
' For each network drive (type=3) write a line to a BAT file
'****

Open szDesktopPath & "\MapMyDrives.BAT" For Output As #1

For Each d In dc
If d.DriveType = 3 Then
l = UCase(d.DriveLetter)
n = UCase(d.ShareName)
' s = s & "NET USE " & l & ": " & n & vbCrLf
Print #1, "NET USE " & l & ": " & n
ListBox1.AddItem "NET USE " & l & ": " & n
End If
Next
Close #1
End Sub

it uses a list box control, but these bits can be chopped out, and the sub renamed to not being a click event
 
Upvote 0
if you want the complete workbook, PM me with your email address and I will send a copy, it also logs the computer name, date, user and drives in a sheet, I have another function where the dos batch file can be recreated for anyone should they lose it, this can then be emailed out to them
 
Upvote 0
Joe

If this was happening to me I think I might have a few questions to ask some people.

Why would there be script to map/unmap drives? Sounds like a nightmare and doesn't sound particularly conducive to productivity.:)
 
Upvote 0
I agree Norie. The reason is for and ERP system that was purchased and added last year. Apparently it's dependant on mapped drives so they place the script to make sure that the those drives are mapped. The solution to use VBA to re-map the drives solves two issues.

One - in case the script doesn't execute properly.
Two - If there is something that I want to add to my list, I can just add it to my excel sheet that is mapping the drives and just run it everyday or whatever. So now I don't have to request new drives added to my script :biggrin:
 
Upvote 0
Post #4 creates a handy batch file to use prior to shifting to a new computer or using an existing computer you have not used before. Mail the batch file to yourself and run it at the new site.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,930
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top