split filename in list box

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

split filename in list box

Post by D Willett »

I have two routines, 1 populates a listbox and the second uses that listbox to copy a file to a folder.
As it is it works just fine but the list is populated with the full path and filename, this kind of looks unprofessional.
I've messed about with it, broke it and put it back together :-) but for cosmetic purposes would like the list to show only the filename.
Once selected the file will be copied to the location in routine 2.
The code has the split function within it but I've looked at it too long and starting to see double....

Routine1:

Code: Select all

Private Sub procDocOther_Click(Index As Integer)
    On Error GoTo cError

    Dim i      As Integer
    Dim myFiles() As String
    Dim myPath As String
    CmnDialog1.FileName = ""

    If Me.txtEst = "" Then
    MsgBox "Please Enter The Job Number in The Main View", vbInformation, "Document Import"
    Exit Sub
    End If
    

    With CmnDialog1
        .MaxFileSize = 32000   'this will max out the buffer for the filenames array for large selections. *NEW*
        .CancelError = False   'if cancel is pressed, the code jumps to cError because of the On Error statement above

        .Filter = "PDF Files|*.pdf"
        .FilterIndex = 2
        .InitDir = ""
        .Flags = CD_FLAGS   'this is where we tell it to use multiselect
        .ShowOpen

        myFiles = Split(.FileName, vbNullChar)   'the Filename returned is delimeted by a null character because we selected the cdlOFNLongNames flag

        Select Case UBound(myFiles)
            Case 0   'if only one was selected we are done
                Dim strFile As String
                Dim intPos As Integer
                ' Get filename with path
                strFile = myFiles(0)
                ' Get position of last backslash
                intPos = InStrRev(strFile, "\")
                ' Extract part after last backslash, i.e. the filename
                strFile = Mid(strFile, intPos + 1)
                ' Add the filename
                frmDocImport.Show
                frmDocImport.txtDocEst = Me.txtEst
                frmDocImport.lstFiles.AddItem myFiles(0)

                
            Case Is > 0   'if more than one, we need to loop through it and append the root directory
            
            frmDocImport.Show
            frmDocImport.txtDocEst = Me.txtEst
                For i = 1 To UBound(myFiles)
                    myPath = myFiles(0) & IIf(Right(myFiles(0), 1) <> "\", "\", "") & myFiles(i)
                    frmDocImport.lstFiles.AddItem myPath

                Next i
        End Select
        CmnDialog1.FileName = ""
    End With
    
    Exit Sub

cError:
    Beep
    MsgBox Err.Description   '*NEW*


End Sub
Routine2:

Code: Select all

Private Sub cmdSaveDoc_Click()
    Dim DocPath As String
    Dim DocType As String
    DocType = InputBox("What Type of Document is This?", "")
    DocPath = "L:\MMPDF\ConsoleFiles\" & Me.txtDocEst & "\"
    
    Call cmdNewFolder_Click

    If DocType = "" Then
        MsgBox "You Must Enter A Document Type", vbInformation, "Document Type"
        Exit Sub
    Else
    
            If MsgBox("This Document Will Be Saved as " & Me.txtDocEst & "-" & DocType & ".pdf", _
            vbYesNo + vbInformation) = vbYes Then
            FileCopy Me.lstFiles, DocPath & Me.txtDocEst & "-" & DocType & ".pdf"
            Kill Me.lstFiles
            

        Else
        End If
    End If
    Unload Me
    Call Form1024Image.cmdList_Click

End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: split filename in list box

Post by HansV »

Change

frmDocImport.lstFiles.AddItem myFiles(0)

to

frmDocImport.lstFiles.AddItem strFile
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: split filename in list box

Post by D Willett »

Hi Hans
That works with a single document selected perfectly but when multple docs are selected through the common dialog the full path and filename is shown:

Code: Select all

       Case Is > 0   'if more than one, we need to loop through it and append the root directory
            
            frmDocImport.Show
            frmDocImport.txtDocEst = Me.txtEst
                For i = 1 To UBound(myFiles)
                    myPath = myFiles(0) & IIf(Right(myFiles(0), 1) <> "\", "\", "") & myFiles(i)
                    frmDocImport.lstFiles.AddItem myPath

                Next i
        End Select
Edited:

I changed it to the following which seems to work:

Code: Select all

       Case Is > 0   'if more than one, we need to loop through it and append the root directory
            
                frmDocImport.Show
                frmDocImport.txtDocEst = Me.txtEst
                For i = 1 To UBound(myFiles)
                    myPath = myFiles(0) & IIf(Right(myFiles(0), 1) <> "\", "\", "") & myFiles(i)
                    frmDocImport.lstFiles.AddItem myFiles(i)

                Next i
        End Select
        CmnDialog1.FileName = ""
    End With
I'll go with this ( but if I've missed something please advise )

Thanks again.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: split filename in list box

Post by HansV »

It seems to me you don't need the line

myPath = myFiles(0) & IIf(Right(myFiles(0), 1) <> "\", "\", "") & myFiles(i)

any more.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: split filename in list box

Post by D Willett »

It seems correct...

Thanks once again Hans.

Regards
Cheers ...

Dave.