Rename files with items from Listbox and textbox from Userform

Doflamingo

Board Regular
Joined
Apr 16, 2019
Messages
238
Hi all,

Here is the code of the Userform I use

Code:
If Me.répertoire = "" Then Me.répertoire = ThisWorkbook.Path
  nf = Dir(Me.répertoire & "\*.*")
  n = 0
  Do While nf <> ""
    n = n + 1
    ReDim Preserve Tbl(1 To n)
    Tbl(n) = nf
    nf = Dir
  Loop
  If n > 0 Then Me.ListBox1.List = Tbl
  'partie retouchée
  Me.TextBox1 = Me.ListBox1.ListCount & IIf(Me.ListBox1.ListCount > 1, " Fichiers", " Fichier")
  Me.TypeFich.List = Array("*.*", "*.xls", "*.jpg", "*.mdb", "*.txt", "*.docx", "*.pdf")
  'utilisation de la variable Enable_event pour éviter la passage dans l'événement Change
  Enable_event = True
  Me.TypeFich.ListIndex = 0
  Enable_event = False
  
    ComboBox1.Clear
    With ComboBox1
    .AddItem "PR_"
    .AddItem "WC_"
    .AddItem "PRegultion_"
    .AddItem "BL_"
    .AddItem "ID_"
    .AddItem "FS_"

textbox3.value = ""
    End With


And here is the code to navigate in the files of your laptop

Code:
If Val(Application.Version) >= 10 Then
   With Application.FileDialog(msoFileDialogFolderPicker)
     .InitialFileName = CurDir()
     .Show
     If .SelectedItems.Count > 0 Then
       Me.répertoire = .SelectedItems(1)
     Else
       Me.répertoire = ""
     End If
     ChDir Me.répertoire
     UserForm_Initialize
    End With
  Else
    DossierChoisi = VoirDossier("Choisir le dossier")
    If DossierChoisi <> "" Then
      Me.répertoire = DossierChoisi
    End If
    ChDir Me.répertoire
    UserForm_Initialize
  End If

it works with a function from a module

Code:
Private Type BROWSEINFO  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type


Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias _
            "SHGetPathFromIDListA" (ByVal pidl As Long, _
            ByVal pszPath As String) As Long
            
Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias _
            "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
            As Long
            
Private Const BIF_RETURNONLYFSDIRS = &H1


Public Function VoirDossier(szDialogTitle As String) As String
  Dim X As Long, bi As BROWSEINFO, dwIList As Long
  Dim szPath As String, wPos As Integer
  
    With bi
        .hOwner = hWndAccessApp
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
    
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
    
    If X Then
        wPos = InStr(szPath, Chr(0))
        VoirDossier = Left$(szPath, wPos - 1)
    Else
        VoirDossier = ""
    End If
End Function

The files appear in Listbox1, and I would like to know how to rename them, when I select one with the combox1 values and the textbox3 values in the code of the userform posted above. But here just a reminder

Code:
ComboBox1.Clear    With ComboBox1
    .AddItem "PR_"
    .AddItem "WC_"
    .AddItem "PRegultion_"
    .AddItem "BL_"
    .AddItem "ID_"
    .AddItem "FS_"


textbox3.value = ""

Any ideas ? :confused:
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I've found the solution with a listbox and a textbox.

Code:
TextBox3.Value = ComboBox2.Value & TextBox5.Value & ComboBox1.Value

now i have to find a way to keep the file extension but it will be in another thread
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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