Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map Network Drive

Status
Not open for further replies.

Kevsim

Instructor
Apr 18, 2000
385
AU
I am looking for code to map a network drive.

I have tried the following but receive error "Object Required".

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "L:", "\\mydrive\share"

and

Set ws = Server.CreateObject("WScript.Network")
ws.MapNetworkDrive "L:", "\\mydrive\share"

I would appreciate some assistance.

kevsim
 
And this ?
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "L:", "\\mydrive\share"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi Kevsim,

PHV is correct but, perhaps, some explanation.

You are asking for an object of type "WScript.Network" to be created. This doesn't just happen, it is an action (or a "Method") that is performed by another, existing, object. In your code you are asking that some object, referenced by WScript, perform this action for you, and you are being told that WScript is not an object.

If you think WScript should be referencing an object then you need to look at the rest of your code to find out why it doesn't. If WScript is just something you've copied from somewhere else and unrelated to the rest of your code then you need, instead, to ask some other object, to which you do have a reference, to create the new object for you. This is what PH's code does - by not explicitly specifying an object, he is requesting the default object, the Application (Word or Excel or whatever you're using) perform the action.

This works in this case because the Application does know how to do it - creating a new object is a fairly generic type action that most objects can perform. In the general case, of course, you must ask an appropriate object to do the action you want. So, for example, the Method you actually want to use, MapNetworkDrive cannot be performed by the Application which is why you have to create a specialised object to do it.

One final note. When you have done with the object you have created you should explicitly destroy it:
Code:
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "L:", "\\mydrive\share"
Set WshNetwork = Nothing

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tony, CreateObject is a VBA function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
My apologies. CreateObject is indeed a VBA Function - a method of VBA and not the host application.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
PHV, TonyJollans,
Thank you both for the information provided.

I tried the code as follows –

Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "L:", "\\mydrive\share"
Set WshNetwork = Nothing
I was not using the correct drive text in this example.

When I reach the second line I receive an error message –
“The local drive is already in use”
I then quit the code and do it manually, all works OK.
Why did I receive the error message?

The manual method is as follows, using the correct drive text –

Windows Explorer\Tools\Map Network Drive
An input window opens and selections made
In the Drive window, I select L \\headoffice\share (this is my default setting)
In the Folder window, I select \\nthoffice\share and select Finish.
I confirm I want to change the drive, the drive changes to what I want.

To bring the drive back to my default setting, I select
In the drive window, I select \\nthoffice\share
In the Folder window, I select L \\headoffice\share and select Finish.
I confirm I want to change the drive, the drive changes to what I want.

I would appreciate advice on the errors I am making with the code.

kevsim
 
Hi Kevsim,

Doing it through the GUI is an interactive process which feeds back information to you and let's you take other actions. In code you must do all this yourself, so the logic you need is something along the lines of:

IF Drive L is already mapped THEN
Ask User
IF User wants to unmap the drive THEN
UnMap It
Map New Drive L
ENDIF
ELSE
Map Drive L
ENDIF

As I suspect you don't want the prompt you can simply go ahead and unmap before remapping. Note that any error on the unmapping is ignored so if there was nothing mapped in the first place it won't cause you a problem:
Code:
[blue]Set WshNetwork = CreateObject("WScript.Network")

On Error Resume Next
WshNetwork.RemoveNetworkDrive "L:"
On Error GoTo 0

WshNetwork.MapNetworkDrive "L:", "\\mydrive\share" 'HardDrive"

Set WshNetwork = Nothing[/blue]

If there is some other (real) error removing the connection, the mapping may fail but normal error trapping will pick this up. You may find you have to add an extra parameter to the Remove to Force it ..
Code:
[blue]    WshNetwork.RemoveNetworkDrive "L:", True[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
TonyJollans,

Thanks for the info, it works great.

kevsim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top