Adding a browse button in VBA Excel
Solved
schont
Posted messages
33
Status
Member
-
Gab -
Gab -
Quel code dois-je utiliser pour ajouter un bouton parcourir dans une userform ?
Je voudrais ajouter un bouton de commande qui utilise la fonction parcourir et lorsque que le fichier désiré est sélection son chemin s'affiche dans une zone de texte se trouvant dans la même userform.
C'est pour lier une référence à fichier pdf.
Si quelqu'un a une idée ...
Merci
Je voudrais ajouter un bouton de commande qui utilise la fonction parcourir et lorsque que le fichier désiré est sélection son chemin s'affiche dans une zone de texte se trouvant dans la même userform.
C'est pour lier une référence à fichier pdf.
Si quelqu'un a une idée ...
Merci
Configuration: Windows XP Internet Explorer 6.0 excel 2003
4 answers
Hello,
The .GetOpenFilename method does not seem to offer that kind of option.
However, I think we can simplify the code a bit:
Best regards,
The .GetOpenFilename method does not seem to offer that kind of option.
However, I think we can simplify the code a bit:
Dim file_name As String file_name = Application.GetOpenFilename file_name = Mid(file_name, InStrRev(file_name, "\") + 1, InStrRev(file_name, ".") - InStrRev(file_name, "\") - 1) Range("a1") = file_name Best regards,
I just noticed that when you click on the browse button and then instead of clicking "open" you click "cancel," an error returns "runtime error '5'" and debugging is proposed.
I tried with
but with that, even when I click "open," the message box opens and displays the message.
Thank you for helping me solve this problem.
I tried with
If FileToOpen = False Then MsgBox "Operation cancelled", vbExclamation Exit Sub End If
but with that, even when I click "open," the message box opens and displays the message.
Thank you for helping me solve this problem.
Good evening, schont,
I don't understand why this error occurs, but a quick and simple solution is to add an error handling line at the beginning of the procedure;
There, that’s fixed!
Best regards
I don't understand why this error occurs, but a quick and simple solution is to add an error handling line at the beginning of the procedure;
Private Sub CommandButton1_Click() On Error Resume Next With Application.FileDialog(msoFileDialogOpen) .AllowMultiSelect = False .Show UserForm1.TextBox1.Text = .SelectedItems(1) End With End Sub
There, that’s fixed!
Best regards
Bonjour,
Is it possible to use the browse button but only retrieve the file name without the full path or the .xlsx extension?
That is to say: "myfile" instead of "C:\folder\myfile.xlsx"
Because so far I have only been able to do a complicated manipulation to isolate the name:
Dim file As String
Dim file1 As String
Dim file2 As String
Dim file3 As String
Dim name As String
file1 = Range("a1").Value
For i = Len(file1) - 5 To 1 Step -1
file2 = Mid(file1, i, 1)
If file2 = "\" Then
GoTo file_name
End If
Next i
file_name: file3 = Mid(file1, i + 1)
file = Left(file3, Len(file3) - 5)
Range("a1") = file
Thank you
Is it possible to use the browse button but only retrieve the file name without the full path or the .xlsx extension?
That is to say: "myfile" instead of "C:\folder\myfile.xlsx"
Because so far I have only been able to do a complicated manipulation to isolate the name:
Dim file As String
Dim file1 As String
Dim file2 As String
Dim file3 As String
Dim name As String
file1 = Range("a1").Value
For i = Len(file1) - 5 To 1 Step -1
file2 = Mid(file1, i, 1)
If file2 = "\" Then
GoTo file_name
End If
Next i
file_name: file3 = Mid(file1, i + 1)
file = Left(file3, Len(file3) - 5)
Range("a1") = file
Thank you
Private Sub CommandButton3_Click()
FileToOpen = Application _
.GetOpenFilename("PDF Files (*.pdf), *.pdf", 1, "Browse...")
If FileToOpen = False Then
MsgBox "Operation canceled", vbExclamation
Exit Sub
End If
' is something missing?!
End Sub
Now, a little question: the file path is copied to my TextBox11 and when I click on a "complete" button, the value of my TextBox is copied to a cell, the code is:
Sheets("Sheet1").Cells(Lg, "I").Value = UserForm1.TextBox11.Value
The file path displays correctly, but not as a link, which is convenient for opening it with a single click.
Is there an option to check in Excel to transform a path into a link (I believe Word does it automatically when you type a web address), or does it have to be done through VBA?
Anyway, kudos and thank you for the speed and efficiency!
You can replace Selection with a fixed cell address.
Best regards