ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   using a macro to map a shared drive (https://www.excelbanter.com/excel-programming/428302-using-macro-map-shared-drive.html)

Popeye the powerman[_2_]

using a macro to map a shared drive
 
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

Dave Peterson

using a macro to map a shared drive
 
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


Popeye the powerman wrote:

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


--

Dave Peterson

Popeye the powerman[_2_]

using a macro to map a shared drive
 
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



"Dave Peterson" wrote:

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


Popeye the powerman wrote:

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


--

Dave Peterson


Dave Peterson

using a macro to map a shared drive
 
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...

Popeye the powerman wrote:

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

"Dave Peterson" wrote:

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


Popeye the powerman wrote:

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


--

Dave Peterson


--

Dave Peterson

Popeye the powerman[_2_]

using a macro to map a shared drive
 
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
"Dave Peterson" wrote:

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...

Popeye the powerman wrote:

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

"Dave Peterson" wrote:

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


Popeye the powerman wrote:

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

--

Dave Peterson


--

Dave Peterson


Dave Peterson

using a macro to map a shared drive
 
I don't see how this would work, but the other not...

But glad you got something working.

Popeye the powerman wrote:

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
"Dave Peterson" wrote:

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...

Popeye the powerman wrote:

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

"Dave Peterson" wrote:

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


Popeye the powerman wrote:

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

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


All times are GMT +1. The time now is 02:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com