Results 1 to 4 of 4

Thread: [RESOLVED] CommonDialog Open and Network Path

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Resolved [RESOLVED] CommonDialog Open and Network Path

    I need to run a CommonDialog Open box to get name of a file on the network from the user, but I'll need the network path (\\server\path\filename), not the mapped path (Z:\filename), since I'm saving the file name for some other user to use at a later time, and different departments are mapped differently. (I know it would be more elegant if everyone on the network was mapped the same, but I don't have the luxury of telling the admin how to run his network.)

    I know I can, say, open a file with either the mapped path or the network path, but for some reason I'm drawing a blank in how to get cdlg to give me a network path when the user clicks on a file. An API I'm forgetting?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: CommonDialog Open and Network Path

    This worked for me just now:
    VB Code:
    1. Private Sub cmdOpen_Click()
    2.   ' CancelError is True.
    3.    On Error GoTo ErrHandler
    4.    CommonDialog1.InitDir = "\\w2k\c550" ' this is the network drive
    5.    ' Set Flags
    6.     CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames
    7.    ' Set filters.
    8.    CommonDialog1.Filter = "All Files (*.*)|*.*|Text" & _
    9.       "Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
    10.    ' Specify default filter.
    11.    CommonDialog1.FilterIndex = 2 ' Default to TEXT
    12.    ' Display the Open dialog box.
    13.    CommonDialog1.ShowOpen
    14.    ' Call the open file procedure.
    15.  '  OpenFile (CommonDialog1.FileName)
    16.    Debug.Print CommonDialog1.FileName
    17.    Exit Sub
    18.  
    19. ErrHandler:
    20. ' User pressed Cancel button.
    21.    Exit Sub
    22.  
    23. End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: CommonDialog Open and Network Path

    You can use the WNetGetConnection function to translate a mapped path into an UNC path.
    VB Code:
    1. Private Declare Function WNetGetConnection _
    2.  Lib "mpr.dll" Alias "WNetGetConnectionA" ( _
    3.     ByVal lpszLocalName As String, _
    4.     ByVal lpszRemoteName As String, _
    5.     ByRef cbRemoteName As Long _
    6. ) As Long
    7.  
    8. Public Function GetUNC(ByVal sMappedPath As String) As String
    9.     Const MAX_PATH As Long = 260&
    10.     Dim sUNC As String
    11.     sUNC = String$(MAX_PATH, vbNullChar)
    12.     If WNetGetConnection(sMappedPath, sUNC, MAX_PATH) = 0 Then
    13.         'Success
    14.         GetUNC = Left$(sUNC, InStr(sUNC, vbNullChar) - 1)
    15.     End If
    16. End Function

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: [RESOLVED] CommonDialog Open and Network Path

    Thank you both. Either solution works nicely, and I learned a lot tonight.

Posting Permissions

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



Click Here to Expand Forum to Full Width