Open file using UNC names and Chdrive

turbotoan

Board Regular
Joined
Feb 25, 2002
Messages
62
Hi,
I need to open a file using the network UNC name instead of the drive letter. What might be drive "P" to one user, may be drive "Q" for another. But both point to "server1".
Can this be done?

I tried using the Chdrive method in the past & it didn't work. What do you think?
Thanks a bunch.
This message was edited by turbotoan on 2002-04-04 18:32
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
On 2002-04-04 18:32, turbotoan wrote:
Hi,
I need to open a file using the network UNC name instead of the drive letter. What might be drive "P" to one user, may be drive "Q" for another. But both point to "server1".
Can this be done?

I tried using the Chdrive method in the past & it didn't work. What do you think?
Thanks a bunch.
This message was edited by turbotoan on 2002-04-04 18:32

As you have found out, neither ChDrive or CHDir recognize UNC paths. You will need to use an API function.

Try something like this.<pre/>

Option Explicit

Private Declare Function SetCurrentDirectoryA Lib "kernel32" _
(ByVal lpPathName As String) As Long
'// Function Discription:
'// :The SetCurrentDirectory function changes the current
'// :grin:irectory for the current process.
'//
'// Parameter Discription:
'// :lpPathName
'// : Points to a null-terminated string that specifies the path
'// : to the new current directory.
'// : This parameter may be a relative path or a fully qualified path.
'// : In either case, the fully qualified path of the specified directory
'// : is calculated and stored as the current directory.
'// :
'// : Succeed Value = NonZero
'// : Fail Value = Zero
'// :

Sub SetDir_UNC()
Dim sServer As String
Dim sIniPath As String
Dim sFullPathFileName As String
Dim oSuccess

'// Set the server variable here
sServer = "\ServerShare"
'// Store the current directory for later restoration.
sIniPath = CurDir

'// Set the new current directory to your UNC path.
oSuccess = SetCurrentDirectoryA(sServer)
'// Check if successfully connected
If oSuccess = 0 Then MsgBox "Unable to connect to " & sServer: Exit Sub

'// Show the GetOpenFilename dialog.
sFullPathFileName = Application.GetOpenFilename

'// Exit if the user cancells the dialog.
If sFullPathFileName = "False" Then Exit Sub
'// Otherwise continue.....

'// Lastly.... restore to the users initial directory.
SetCurrentDirectoryA sIniPath

End Sub</pre>
 
Upvote 0

Forum statistics

Threads
1,224,226
Messages
6,177,275
Members
452,765
Latest member
Erka Gizli

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top