We help IT Professionals succeed at work.

spaces in file name / common dialog control vb6

rjef
rjef asked
on
I am using the common dialog control in VB6.  When the user selects a file that is in a subdirectory like 'c:\My Documents\info.txt'  I do not get the complete file name back and the program crashes.  Because it can not find the file name.  I have run it on some WINNT systems and it works but on others it doesn't.  The systems are all running the same OCX versions.  I think it is failing because of the space between "MY" and "Documents"  Any ideas why this would happen?
Comment
Watch Question

Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi rjef,

That is not correct where you can't select a file from "My Documents"! Please check your codes again.

FYI: Tested on Win2000, VB6 no problem at all.
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
What's the Error message when your application crashed?

Author

Commented:
Actually it doesn't crash when I select the file.  It crashes when I use the filecopy command.

I am not at the system where it is crashing at right now.  I will see on friday what the error number is
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Try this:

Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260

Private Type BrowseInfo
   hwndOwner      As Long
   pIDLRoot       As Long
   pszDisplayName As Long
   lpszTitle      As Long
   ulFlags        As Long
   lpfnCallback   As Long
   lParam         As Long
   iImage         As Long
End Type

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long

Public Function OpenDirectoryTV(odtvOwner As Form, Optional odtvTitle As String) As String
   Dim lpIDList As Long
   Dim sBuffer As String
   Dim szTitle As String
   Dim tBrowseInfo As BrowseInfo
   szTitle = odtvTitle
   With tBrowseInfo
      .hwndOwner = odtvOwner.hwnd
      .lpszTitle = lstrcat(szTitle, "")
      .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
   End With
   lpIDList = SHBrowseForFolder(tBrowseInfo)
   If (lpIDList) Then
      sBuffer = Space(MAX_PATH)
      SHGetPathFromIDList lpIDList, sBuffer
      sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
      OpenDirectoryTV = sBuffer
   End If
End Function

Private Sub Command1_Click()
    CommonDialog1.FileName = ""
    CommonDialog1.ShowOpen
    If CommonDialog1.FileName <> "" Then Text1.Text = CommonDialog1.FileName
End Sub

Private Sub Command2_Click()
    Text2.Text = OpenDirectoryTV(Me, "Select a Directory")
End Sub

Private Sub Command3_Click()
    FileCopy Text1.Text, Text2.Text & "\" & Dir(Text1.Text)
    MsgBox "Finished"
End Sub

Private Sub Form_Load()
    Command1.Caption = "Open Source"
    Command2.Caption = "To"
    Command3.Caption = "Copy Now"
End Sub

regards,
ryancys

Author

Commented:
I have heard of the using the API calls but I was trying to avoid having to do this.  I will check it out on Friday.  Can you explain why it does work on some system but not on others.  All of them using WINNT sp6
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
I only can answer you base on the error message we got, from there we can analysis what cause the error.
Software Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT
Commented:
and for your information, you can replace the API above which it's used to select a Folder for the current machine.

Commented:
There is a catch...

If your common dialog is set (flags=xxx) for multiple file selection, then YOU have to manage the parsing (separation) of the multipe filenames.

However if multi selection is NOT enabled, then the returned .filename is complete, and should work normally.

Good luck
CERTIFIED EXPERT
Author of the Year 2009

Commented:
Hi rjef,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: ryancys and mcoop

rjef, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer

Commented:
Finalized as proposed

mcoop points at:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20434695.html

modulo

Community Support Moderator
Experts Exchange