Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 2 of 2

Thread: Open file using UNC names and Chdrive

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Wisconsin - USA
    Posts
    62
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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 ]

  2. #2
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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.



    Option Explicit

    Private Declare Function SetCurrentDirectoryA Lib "kernel32" _
    (ByVal lpPathName As String) As Long
    '// Function Discription:
    '// :The SetCurrentDirectory function changes the current
    '// 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





    Kind Regards,
    Ivan F Moala From the City of Sails

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •