Catch the name that is chosen using dialog box

DeThomaso

New Member
Joined
Nov 14, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi
This is my first question on this forum. I am also rather new at VBA.

I want to catch the name of the file that is chosen by the dialog popup window using this line of code:
"RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")"
As I have interpreted it filter the directory so only teextfiles are seen.
RetVal variable returns a Boolean, which is just used to see if the user is actually picking a file or just click cancel.
The original code in itself is not written by me, I just want to update it to display the particular file name that is chosen in a specific cell in the Excell workbook i.e. A1.
For the moment I just want to catch the name of the file that the user is clicking on when message box comes up.
Is there an extension to the code line above or do I need to rewritten the whole code to first catch the filename and then do the rest of the code?

Tanks ahead
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
The code in the beginning looks like this:

VBA Code:
Sub Import_ReferenceFile()
 Dim DestBook As Workbook, SourceBook As Workbook
 Dim DestCell As Range
 Dim RetVal As Boolean
 Dim PickFilename As String


 ' Turn off screen updating.
 Application.ScreenUpdating = False
 With Worksheets("Jura DFLASH_1pA4_Ref").Select
 Range("A3:A65536").ClearContents
 End With

 ' Set object variables for the active book and active cell.
 Set DestBook = ActiveWorkbook
 'Set DestCell = ActiveCell
 Set DestCell = Range("A4", "A300")

 'MsgBox "Please click OK and navigate to your Text File"
 ' Show the Open dialog box.
 RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")
 ' If Retval is false (Open dialog canceled), exit the procedure.
 If RetVal = False Then Exit Sub[CODE=vba]
[/CODE]
 
Upvote 0
I added the variable PickFilename to try to catch the name but I cannot work it out...

/Thanks
 
Upvote 0
Try the GetOpenFilename method of the Application object instead . . .

VBA Code:
    Dim vFileName As Variant
    vFileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv), *.csv", Title:="Select a File")
   
    If vFileName = False Then Exit Sub

    MsgBox "User selected " & vFileName, vbInformation

Hope this helps!
 
Upvote 0
Solution
Hi again Domenic, if still availble...
What is the purpose of the last line in your code as below?

VBA Code:
MsgBox "User selected " & vFileName, vbInformation
 
Upvote 0
Try the GetOpenFilename method of the Application object instead . . .

VBA Code:
    Dim vFileName As Variant
    vFileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv), *.csv", Title:="Select a File")
  
    If vFileName = False Then Exit Sub

    MsgBox "User selected " & vFileName, vbInformation

Hope this helps!

This works but I get problems with Exit Sub statement if I choose a file the code gets error and want me to debug the code.
The strange part it has worked and now it does not. I have seen this before as if there is a timing issue.
When I press cancel it works fine as it should but when I choose a file it stops.
I was thinking if the file string containing charactors that is not allowed or something.
Here is the string when I have chosen a file:

C:\Temp\RMA0199-1_PST_Jura_Dump_BFLASH1.S

Is there anything wrong with the charactors in this string causing the Exit Sub to stop?
 
Upvote 0
Hi again Domenic, if still availble...
What is the purpose of the last line in your code as below?

VBA Code:
MsgBox "User selected " & vFileName, vbInformation
I know now what it is..stupid me. A message popup window showing the chosen file info...
But it was actually the Exit Sub I had problem with so this confused me a little bit.
:)
 
Upvote 0
Case closed All Good.
variable PickFilename or in your case vFileName needed to declared as Variant and not String.
:)
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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