By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

How to "Map" a Network drive using Visual Basic

P: n/a
I have written the following VBScript program. It is stored into a
file "map_drive.vbs". It successfully mapped to a network drive
\\server1\data.

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
sPwd = inputbox("Enter password")
WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
"xyz_net\John", sPwd
Msgbox "Drives has been map successful"

Could anyone teach me how to convert it to a Visual Basic program such
that it can be compiled to an .exe file?
I tried to use Active X Control – Windows Script Control 1.0. But I
couldn't make it work. Perhaps, I do not know how to use the Script
Control, or it is a totally wrong approach. It will be nice if you can
give Step-by-step instructions.
Please help. Thanks a lot.
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
No idea for the wscript object, but I can give you the API code for VB to
map/unmap drives and a whack of others ..
http://vbnet.mvps.org/code/network/netconnect.htm

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13**************************@posting.google.c om...
: I have written the following VBScript program. It is stored into a
: file "map_drive.vbs". It successfully mapped to a network drive
: \\server1\data.
:
: Dim WshNetwork
: Set WshNetwork = WScript.CreateObject("WScript.Network")
: sPwd = inputbox("Enter password")
: WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
: "xyz_net\John", sPwd
: Msgbox "Drives has been map successful"
:
: Could anyone teach me how to convert it to a Visual Basic program such
: that it can be compiled to an .exe file?
: I tried to use Active X Control  Windows Script Control 1.0. But I
: couldn't make it work. Perhaps, I do not know how to use the Script
: Control, or it is a totally wrong approach. It will be nice if you can
: give Step-by-step instructions.
: Please help. Thanks a lot.

Jul 17 '05 #2

P: n/a
Alex, it very similar to the vbscript you have already,

Dim WshNetwork ' COULD SET AS TYPE: OBJECT otherwise its a variant
Set WshNetwork = CreateObject("WScript.Network") ' THIS LINE DIFFERS
SLIGHTLY: Note the CreateObject
msgbox("Enter password")

WshNetwork.MapNetworkDrive "J:", "\\server1\data", False, "xyz_net\John",
sPwd
MsgBox "Drives has been map successful"

Set WshNetwork = Nothing ' BEST TO RELEASE THE OBJECT (FREE UP RESOURSES)

hope it helps
al.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13**************************@posting.google.c om...
I have written the following VBScript program. It is stored into a
file "map_drive.vbs". It successfully mapped to a network drive
\\server1\data.

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
sPwd = inputbox("Enter password")
WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
"xyz_net\John", sPwd
Msgbox "Drives has been map successful"

Could anyone teach me how to convert it to a Visual Basic program such
that it can be compiled to an .exe file?
I tried to use Active X Control - Windows Script Control 1.0. But I
couldn't make it work. Perhaps, I do not know how to use the Script
Control, or it is a totally wrong approach. It will be nice if you can
give Step-by-step instructions.
Please help. Thanks a lot.

Jul 17 '05 #3

P: n/a
Thanks Alan, my program can run now. :)
Thanks Randy for your suggestion too.

I have another question regarding Dim. Why can't I do the following.
Dim WshNetwork As WScript.Network
‘Compile Error: User Defined Type Not Define

However the following is all right.
Dim WshNetwork As Object ‘NO error.
Set WshNetwork = CreateObject("WScript.Network")

I was trying the above because I did something similar that make use
of "Excel" object. The VB code is as follows:
Dim XLObj As Excel.Application
Set XLObj = CreateObject("Excel.Application")

To make the above VB code run without error, I also need
to configure the VB project to include "Excel" as follows.
1. Project>References to open the References dialog box.
2. select "Microsoft Excel 9.0 object library"

I did not manage to find a corresponding object library (i.e.
WScript.Network) in VB>Project>Reference.
I feel that it is important to understand this to improve my
programming skill and write better program. Pls help. Thank you very
much.

"Alan Barker" <Az*****@hotmailREMOVEME.com> wrote in message news:<c9**********@sparta.btinternet.com>...
Alex, it very similar to the vbscript you have already,

Dim WshNetwork ' COULD SET AS TYPE: OBJECT otherwise its a variant
Set WshNetwork = CreateObject("WScript.Network") ' THIS LINE DIFFERS
SLIGHTLY: Note the CreateObject
msgbox("Enter password")

WshNetwork.MapNetworkDrive "J:", "\\server1\data", False, "xyz_net\John",
sPwd
MsgBox "Drives has been map successful"

Set WshNetwork = Nothing ' BEST TO RELEASE THE OBJECT (FREE UP RESOURSES)

hope it helps
al.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13**************************@posting.google.c om...
I have written the following VBScript program. It is stored into a
file "map_drive.vbs". It successfully mapped to a network drive
\\server1\data.

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
sPwd = inputbox("Enter password")
WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
"xyz_net\John", sPwd
Msgbox "Drives has been map successful"

Could anyone teach me how to convert it to a Visual Basic program such
that it can be compiled to an .exe file?
I tried to use Active X Control - Windows Script Control 1.0. But I
couldn't make it work. Perhaps, I do not know how to use the Script
Control, or it is a totally wrong approach. It will be nice if you can
give Step-by-step instructions.
Please help. Thanks a lot.

Jul 17 '05 #4

P: n/a
Alex Ang wrote:
Thanks Alan, my program can run now. :)
Thanks Randy for your suggestion too.

I have another question regarding Dim. Why can't I do the following.
Dim WshNetwork As WScript.Network
‘Compile Error: User Defined Type Not Define
Select "Windows Scripting Host ..." in Project -> References.

Change your code to

Dim WshNetwork As WshNetwork
Set WshNetwork = New WshNetwork

or shorter:

Dim WshNetwork As New WshNetwork
However the following is all right.
Dim WshNetwork As Object ‘NO error.
Set WshNetwork = CreateObject("WScript.Network")

I was trying the above because I did something similar that make use
of "Excel" object. The VB code is as follows:
Dim XLObj As Excel.Application
Set XLObj = CreateObject("Excel.Application")
For shorter,
Dim XLObj As New Excel.Application
To make the above VB code run without error, I also need
to configure the VB project to include "Excel" as follows.
1. Project>References to open the References dialog box.
2. select "Microsoft Excel 9.0 object library"
CreateObject() just makes it possible to use objects only at run-time
without the reference library at design time. There is no practical
difference when the program is running.
I did not manage to find a corresponding object library (i.e.
WScript.Network) in VB>Project>Reference.
I feel that it is important to understand this to improve my
programming skill and write better program. Pls help. Thank you very
much.


To use WScript.* and Scripting.* objects, reference "Windows Scripting
Host".

--
Olof Lagerkvist
ICQ: 724451
Web page: http://here.is/olof

Jul 17 '05 #5

P: n/a
Alex, you can reference the Wsh Object just like you did with the Excel
object. The reference is
Windows Script Host Object Model
and can be found lurking towards the bottom of the references list.

Its a little different than using the createobject method earlier but with
the Intellisense now working (because you have told VB what type of object
it is) you will find it a lot easier.
The network object is WshNetwork so; your code should now resemble

Dim a As WshNetwork

Set a = New WshNetwork
a.MapNetworkDrive "I:", \\server\folder, False, "domain\username",
"password"

Set a = Nothing ' clean up

------------------------
to further your understanding, when you use the createobject function it
asks the system to do a lookup for any installed ActiveX components with the
name 'wscript.network'. This list is in the HKey Classes Root hive of the
registry (do a search with regedit). In this key it will point to the
wshom.ocx in the system folder. This file is the same file you 'reference'
when you use the alternate method above.

Because you already 'reference' the wshom.ocx you need not call the
createobject function: you are telling VB in advance what object you are
going to create, this is known as early binding.

both methods have the same result but the one above is much faster.

if you need any further info just ask,
al.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13*************************@posting.google.co m...
Thanks Alan, my program can run now. :)
Thanks Randy for your suggestion too.

I have another question regarding Dim. Why can't I do the following.
Dim WshNetwork As WScript.Network
'Compile Error: User Defined Type Not Define

However the following is all right.
Dim WshNetwork As Object 'NO error.
Set WshNetwork = CreateObject("WScript.Network")

I was trying the above because I did something similar that make use
of "Excel" object. The VB code is as follows:
Dim XLObj As Excel.Application
Set XLObj = CreateObject("Excel.Application")

To make the above VB code run without error, I also need
to configure the VB project to include "Excel" as follows.
1. Project>References to open the References dialog box.
2. select "Microsoft Excel 9.0 object library"

I did not manage to find a corresponding object library (i.e.
WScript.Network) in VB>Project>Reference.
I feel that it is important to understand this to improve my
programming skill and write better program. Pls help. Thank you very
much.

"Alan Barker" <Az*****@hotmailREMOVEME.com> wrote in message

news:<c9**********@sparta.btinternet.com>...
Alex, it very similar to the vbscript you have already,

Dim WshNetwork ' COULD SET AS TYPE: OBJECT otherwise its a variant
Set WshNetwork = CreateObject("WScript.Network") ' THIS LINE DIFFERS
SLIGHTLY: Note the CreateObject
msgbox("Enter password")

WshNetwork.MapNetworkDrive "J:", "\\server1\data", False, "xyz_net\John", sPwd
MsgBox "Drives has been map successful"

Set WshNetwork = Nothing ' BEST TO RELEASE THE OBJECT (FREE UP RESOURSES)
hope it helps
al.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13**************************@posting.google.c om...
I have written the following VBScript program. It is stored into a
file "map_drive.vbs". It successfully mapped to a network drive
\\server1\data.

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
sPwd = inputbox("Enter password")
WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
"xyz_net\John", sPwd
Msgbox "Drives has been map successful"

Could anyone teach me how to convert it to a Visual Basic program such
that it can be compiled to an .exe file?
I tried to use Active X Control - Windows Script Control 1.0. But I
couldn't make it work. Perhaps, I do not know how to use the Script
Control, or it is a totally wrong approach. It will be nice if you can
give Step-by-step instructions.
Please help. Thanks a lot.

Jul 17 '05 #6

P: n/a
Alan, Thanks for the explanation. :) Now, I can tell the differences
in VB code between "late binding" and "early binding".

However, I'm afraid I still do NOT understand the following.
Example 1:
Why it should be -
Dim a As WshNetwork
NOT - Dim as As WScript.Network ‘user define type error

Example 2: The Excel object can be Dim in both ways without errors.
Dim XLobj As Excel.Application
‘Dim XLobj As Application is ok too.

Set XLObj = New Excel.Application
‘Set XLObj = New Application is ok too

Why does Excel.Application can be Dim in either ways? Whereas
WshNetwork cannot?

Based on "Windows Script v5.6" documentation" download from MS web
sit, I thought it should be "WScript.Network". This is because the
documentation shows the following diagram for the "Windows Script Host
Object Model". See diagram below.

The Windows Script Host object model consists of 14 objects.
The root object is the WScript object.

WScript
!
+-----WshArguments
!
+-----WshController
!
+-----WshNetwork

Based on the above diagram, I will write the following VB code.
WScript.WshNetwork
because WScript is the ROOT and it contains WshNetwork.

Note: I have this "understanding" of writing VB code in
objectX.objectY.property - "Object hierarchy" (nested object) - from
some ADO programming experience such as the following:
adoFriends.Recordset.MoveLast

I guess I do NOT have adequate understanding on "Object Hierarchy"
etc. Hence, it gives me many problems when writing VB code that duels
with Objects hierarchy, etc.
Pls help. Thanks a lot.

"Alan Barker" <Az*****@hotmailREMOVEME.com> wrote in message news:<c9**********@sparta.btinternet.com>...
Alex, you can reference the Wsh Object just like you did with the Excel
object. The reference is
Windows Script Host Object Model
and can be found lurking towards the bottom of the references list.

Its a little different than using the createobject method earlier but with
the Intellisense now working (because you have told VB what type of object
it is) you will find it a lot easier.
The network object is WshNetwork so; your code should now resemble

Dim a As WshNetwork

Set a = New WshNetwork
a.MapNetworkDrive "I:", \\server\folder, False, "domain\username",
"password"

Set a = Nothing ' clean up

Jul 17 '05 #7

P: n/a
> However, I'm afraid I still do NOT understand the following.
Example 1:
Why it should be -
Dim a As WshNetwork
NOT - Dim as As WScript.Network 'user define type error

Example 2: The Excel object can be Dim in both ways without errors.
Dim XLobj As Excel.Application
'Dim XLobj As Application is ok too.

Set XLObj = New Excel.Application
'Set XLObj = New Application is ok too

Why does Excel.Application can be Dim in either ways? Whereas
WshNetwork cannot? Based on "Windows Script v5.6" documentation" download from MS web
sit, I thought it should be "WScript.Network". This is because the
documentation shows the following diagram for the "Windows Script Host
Object Model". See diagram below.

The Windows Script Host object model consists of 14 objects.
The root object is the WScript object.

WScript
!
+-----WshArguments
!
+-----WshController
!
+-----WshNetwork

Based on the above diagram, I will write the following VB code.
WScript.WshNetwork
because WScript is the ROOT and it contains WshNetwork.

Note: I have this "understanding" of writing VB code in
objectX.objectY.property - "Object hierarchy" (nested object) - from
some ADO programming experience such as the following:
adoFriends.Recordset.MoveLast

I guess I do NOT have adequate understanding on "Object Hierarchy"
etc. Hence, it gives me many problems when writing VB code that duels
with Objects hierarchy, etc.
Pls help. Thanks a lot.
<snip>

when you use the createobject function you need specify what COM server you
are starting and what object you are going create from that server.
so in this case using the createobject method you will specify the COM
server: Wscript and then the Object from the server: WshNetwork hence
Wscript.WshNetwork.
however; when using visual basic you create a 'reference' to the WScript
object in the references page. The object may be Called 'Windows Script Host
Object Model' and have a filename of wshom.ocx but it is the Wscript object.
Because you have now added a reference to this object within VB you no
longer need to prefix any of its objects with the name so Wscript.WshNetwork
becomes WshNetwork.

CreateObject does not use the look at the references in VB so you need still
need to provide the full object hierarchy.
As for the Excel example; This is a little more tricky. You are correct,
declaring Excel.Application or just Application on it's own has the same
effect; this is because although Excel appears in the list of types when you
declare it cannot be declared alone i.e. Dim obj as Excel. The reason for
this is that Excel here is a Type or object library *containing* the type
Application. Also because the Excel object is now referenced in VB it also
just displays the Excel Objects. My bet is if you were to also add a
reference to MS Word in the project as well you would have TWO 'Application'
Types listed Together. You would not be able to distinguish the two (and
neither would VB) try it:

Add a reference to MS Word and MS Excel

Dim ExcelObj As New Application '<- Select first from type list
Dim WordObj As New Application '<- select second from type list

Debug.Print ExcelObj.Name
Debug.Print WordObj.Name

You will notice the same name, so change the declares to

Dim ExcelObj As New Excel.Application
Dim WordObj As New Word.Application

Now you have the desired result.

Your object hierarchy understanding is perfectly ok.

finally, returning to Why it should be -
Dim a As WshNetwork
NOT - Dim as As WScript.Network 'user define type error


it can be, but...
unlike the Excel Library above, for some reason Microsoft did not name
the WScript library 'WScript' but Instead named it 'IWshRuntimeLibrary' !

so (eventually)
Dim obj as new IWshRuntimeLibray.WshNetwork

--------------------------------
Hope I didn't confuse,bore,over complicate etc
any further info just ask,
Apologies for the size of post,

Al.
Jul 17 '05 #8

P: n/a
Alan, Thanks for the explanation. :) Now, I can tell the differences
in VB code between "late binding" and "early binding".

However, I'm afraid I still do NOT understand the following.
Example 1:
Why it should be -
Dim a As WshNetwork
NOT - Dim as As WScript.Network ‘user define type error

Example 2: The Excel object can be Dim in both ways without errors.
Dim XLobj As Excel.Application
‘Dim XLobj As Application is ok too.

Set XLObj = New Excel.Application
‘Set XLObj = New Application is ok too

Why Excel.Application can be Dim in either ways? Whereas WshNetwork
cannot?

Based on "Windows Script v5.6" documentation" download from MS web
sit, I thought it should be "WScript.Network". This is because the
documentation shows the following diagram for the "Windows Script Host
Object Model".

The Windows Script Host object model consists of 14 objects.
The root object is the WScript object.

WScript
!
+-----WshArguments
!
+-----WshController
!
+-----WshNetwork

Based on the above diagram, I will write the following VB code.
WScript.WshNetwork
because WScript is the ROOT and it contains WshNetwork.

Note: I have this "understanding" of writing VB code in
objectX.objectY.property - "Object hierarchy" (nested object) - from
some ADO programming experience such as the following:
adoFriends.Recordset.MoveLast

I guess I do NOT have adequate understanding on "Object Hierarchy"
etc. Hence, it gives me many problems when writing VB code that duels
with Objects hierarchy, etc.
Pls help. Thanks.

"Alan Barker" <Az*****@hotmailREMOVEME.com> wrote in message news:<c9**********@sparta.btinternet.com>...
Alex, you can reference the Wsh Object just like you did with the Excel
object. The reference is
Windows Script Host Object Model
and can be found lurking towards the bottom of the references list.

Its a little different than using the createobject method earlier but with
the Intellisense now working (because you have told VB what type of object
it is) you will find it a lot easier.
The network object is WshNetwork so; your code should now resemble

Dim a As WshNetwork

Set a = New WshNetwork
a.MapNetworkDrive "I:", \\server\folder, False, "domain\username",
"password"

Set a = Nothing ' clean up

------------------------
to further your understanding, when you use the createobject function it
asks the system to do a lookup for any installed ActiveX components with the
name 'wscript.network'. This list is in the HKey Classes Root hive of the
registry (do a search with regedit). In this key it will point to the
wshom.ocx in the system folder. This file is the same file you 'reference'
when you use the alternate method above.

Because you already 'reference' the wshom.ocx you need not call the
createobject function: you are telling VB in advance what object you are
going to create, this is known as early binding.

both methods have the same result but the one above is much faster.

if you need any further info just ask,
al.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13*************************@posting.google.co m...
Thanks Alan, my program can run now. :)
Thanks Randy for your suggestion too.

I have another question regarding Dim. Why can't I do the following.
Dim WshNetwork As WScript.Network
'Compile Error: User Defined Type Not Define

However the following is all right.
Dim WshNetwork As Object 'NO error.
Set WshNetwork = CreateObject("WScript.Network")

I was trying the above because I did something similar that make use
of "Excel" object. The VB code is as follows:
Dim XLObj As Excel.Application
Set XLObj = CreateObject("Excel.Application")

To make the above VB code run without error, I also need
to configure the VB project to include "Excel" as follows.
1. Project>References to open the References dialog box.
2. select "Microsoft Excel 9.0 object library"

I did not manage to find a corresponding object library (i.e.
WScript.Network) in VB>Project>Reference.
I feel that it is important to understand this to improve my
programming skill and write better program. Pls help. Thank you very
much.

"Alan Barker" <Az*****@hotmailREMOVEME.com> wrote in message

news:<c9**********@sparta.btinternet.com>...
Alex, it very similar to the vbscript you have already,

Dim WshNetwork ' COULD SET AS TYPE: OBJECT otherwise its a variant
Set WshNetwork = CreateObject("WScript.Network") ' THIS LINE DIFFERS
SLIGHTLY: Note the CreateObject
msgbox("Enter password")

WshNetwork.MapNetworkDrive "J:", "\\server1\data", False, "xyz_net\John", sPwd
MsgBox "Drives has been map successful"

Set WshNetwork = Nothing ' BEST TO RELEASE THE OBJECT (FREE UP RESOURSES)
hope it helps
al.
"Alex Ang" <Al*******@yahoo.com.sg> wrote in message
news:13**************************@posting.google.c om...
> I have written the following VBScript program. It is stored into a
> file "map_drive.vbs". It successfully mapped to a network drive
> \\server1\data.
>
> Dim WshNetwork
> Set WshNetwork = WScript.CreateObject("WScript.Network")
> sPwd = inputbox("Enter password")
> WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
> "xyz_net\John", sPwd
> Msgbox "Drives has been map successful"
>
> Could anyone teach me how to convert it to a Visual Basic program such
> that it can be compiled to an .exe file?
> I tried to use Active X Control - Windows Script Control 1.0. But I
> couldn't make it work. Perhaps, I do not know how to use the Script
> Control, or it is a totally wrong approach. It will be nice if you can
> give Step-by-step instructions.
> Please help. Thanks a lot.

Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.