×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Map Network Drive

Map Network Drive

Map Network Drive

(OP)
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

RE: Map Network Drive

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

RE: Map Network Drive

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

RE: Map Network Drive

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

RE: Map Network Drive

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

RE: Map Network Drive

(OP)
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

RE: Map Network Drive

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

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

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

    WshNetwork.RemoveNetworkDrive "L:", True

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

RE: Map Network Drive

(OP)
TonyJollans,

Thanks for the info, it works great.

kevsim

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close