How to Connect a network Drive

A

Alain-79

Using fso, I need to be able to make a getFolder on several possible path
including Network Drives... but when one of them is not connected I get an
error...

It's rather simple to open the explorer and to click on this network drive
in order toget it connected... but how can I automate this through vba code...

In other words: How can I through vba connect an existing network drive and
this even if I have just the logical path such as Z:\Folder

Using <<Net Use>> would imply I have kept the network path such as
\\server\...\shared folder or at least that I am capable of recovering it...

Thanks for any potential help...
Regards
Alain
 
J

Jacob Skaria

Hi Alain

---If you are looking to Get the folder object you dont need to map that as
a drive..Instead you can directly mention the UNC Path as below

Dim fso As Object, objFolder As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder("\\server\share folder")
MsgBox objFolder.Name


---If you are looking to map this folder as a network drive then try the below

Dim objWSNet As Object, strDrive As String, strUNCPath As String

Set objWSNet = CreateObject("WScript.Network")
strDrive = "Z:"
strUNCPath = "\\server\share folder"
objWSNet.MapNetworkDrive strDrive, strUNCPath
 
J

joel

A Z:\file you should be able to access like any drive if you have th
correct priviledges. If you can open the file useing a window explore
then you should be able to just use getfile(). I don't know what th
problem you are having but I would first try your code on a file in th
d: drive just to make sure the code is working properly.

Some network files may no have local privildedges for you to access th
file dirrectly. they may have the same prilideges as a Web file
because some files may be capable of being accessed through th
internet. You may need to use FTP (File Transfer Protocol) to acces
the files which is used get get files and directories over th
Internet.

It would help to know what your intentions are going to be to get yo
to use the corect protocol. the correct methos may be differe
depending if you were going to open the file/ Get the files size, ge
file priviledges, etc
 
A

Alain-79

I know I can do that but the problem is that up to now I have not kept the
absolute path starting with \\server\...

I have just made a list of potential path that could be used but those path
are just kept whith their Network Drive letter, not with their full path...
And then when then Network Drive my users will like to use is not connected I
have no way up to now to provoce the re-connection, not no way to recover the
full path... except if this last one would be available somewhere like in a
Registry key...

The unique solution I can imagine at the moment is to keep a memo of the
full path in // to the logical path at the time it is entered in my list...
Then I will be able to reconnect it using net use or any other possible
function for that task...

Thanks for your input
Alain

PS regarding your code proposal
thanks to remember I cannot access the full path
mine is a follow

set fso = CreateObject("Scripting.FileSystemObject")
set objFolder = GetFolder(Z:\SharedFolder)
Z is here a map drive I created through Explorer

the <<set objFolder>> code line produces an error if the Network Drive is
not connected which is systematically the case just after login...
If you go to the explorer and click inside the Z drive => it open it
Then the same code lines will not produce any errors...
 
A

Alain-79

Hi Joel, my code works in many circumstances... and even the part with which
I am facing problems do not produce any errors as soon as the Network drive
is "Connected"

My current problem would rather be to be said as follow

1 - I have saved somewhere a path based on a Network Drive - let say Z:\Folder
2 - I have not savec the full path starting with \\server\...\Folder

3 - After a while or after a logoff - logon, the Network Drive is not
connected any more - you can see that in the explorer when looking to "My
Computer"... all Network drive present either the status "Disconnected
Network Drive" or "Network Drive" only for all connected ones...
4 - if at that moment I run those code lines I get an error
- set fso = CreatObject("Scripting.FileSystemObject")
- Set folder = fso.GetFolder(Z:\Folder)

I know that I could have it working if I could access the full path but do
not know how to recover it on a Disconnected Network Drive ? in a registry
key ?

Thanks for your help
Alain
 
J

Jacob Skaria

There is always a possibility that the user change the drive mapping to
something else. So it is always better to use the UNC path either stored in
an .ini file or if in VBA stored in a dedicated sheet/cell so that you dont
need to be 'drive letter' dependent.
 
A

Alain-79

yes I know that too but on a daily basis they are
1 - not changing such things so often
2 - used to work with the Network Drive letter rather than with the absolute
path they even do not know at all

This is what I consider I should continue to present them what they know the
most...

Thanks once more to you
Regards
Alain
 
A

Alain-79

Here is the answer I found...

At the time I need to to the getFolder on a path (pathToCheck) based on a
network drive (Z) that can be disconnected,
- I first recover the remotePath from the registry key...
Dim oShell As Object: dim remPath$
Set oShell = CreateObject("WScript.Shell")
remPath= oShell.RegRead("HKEY_CURRENT_USER\Network\Z\RemotePath")
- second I map it again - this will re-connect in case not
Dim objWSNet As Object, strDrive As String, strUNCPath As String
Set objWSNet = CreateObject("WScript.Network")
objWSNet.MapNetworkDrive Z:, remPath
- then I can do my getFolder which will fail only if I am not connected to
the network...
fld = fso.GetFolder(pathToCheck)


Thanks to the ones who answered...
Regards
Alain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top