PDA

View Full Version : [SOLVED:] How to save a user selected file as a variable?



Zuccj
06-23-2017, 08:42 AM
In the middle of creating an application, and part of it is that a file will have to be loaded in. The file won't always be the same file, so I just have the folder location open up and let the user select the config file that will be used.


Dim fopen As FileDialog
Set fopen = Application.FileDialog(msoFileDialogOpen)
fopen.Title = "Select Config File"
fopen.InitialFileName = "C:\......"
fopen.Show

Is the current code I have and that works perfectly fine. It opens right up to the correct folder. I've tried a few different ways to get the file to save to a variable, but at the moment haven't had much luck. The file doesn't need to be displayed I just need it saved into a variable so I can manipulate it as needed.


Dim fopen As FileDialog
Dim fcontent As String
Dim textfile As Integer

Set fopen = Application.FileDialog(msoFileDialogOpen)
fopen.Title = "Select Config File"
fopen.InitialFileName = "C:...."
fopen.Show
textfile = FreeFile
Open EvenLogDir & fopen For Input As #textfile
fcontent = Input(LOF(textfile), textfile)

Close #textfile

I'm working with this, and now I'm getting a run-time error 53: File not Found. Not sure how I'm getting that as I'm clicking on the exact file for it to load it, so it should have the exact path.

Paul_Hossler
06-23-2017, 09:29 AM
I commented out some lines to test, but

1. I think you want the fopen.SelectedItems(1) line tp save the selected file name

2. fopen.InitialFileName = "C:...." should probably be something like fopen.InitialFileName = Environ ("USERPROFILE") & "\Documents\*.cfg"




Option Explicit
Sub test()
Dim fopen As FileDialog
Dim fcontent As String
Dim textfile As Integer

ChDir Environ("USERPROFILE") & "\Documents"
Set fopen = Application.FileDialog(msoFileDialogOpen)
fopen.Title = "Select Config File"

' fopen.InitialFileName = "C:...."
fopen.Show

MsgBox fopen.SelectedItems(1) ' <<<<<<<<<<<<<<<

'textfile = FreeFile
'Open EvenLogDir & fopen For Input As #textfile
'fcontent = Input(LOF(textfile), textfile)
'Close #textfile
End Sub




FWIW, I usually just use .GetOpenFileName. For some reason I just find it easier to use



Option Explicit
Sub test2()
Dim FileNameToOpen As String

ChDir Environ("USERPROFILE") & "\Documents"

'https://msdn.microsoft.com/VBA/Excel-VBA/articles/application-getopenfilename-method-excel
FileNameToOpen = Application.GetOpenFilename("Excel Files (*.xls?), *.xls?, Text Files (*.txt), *.txt", 2, "Get a File Name", , False)

If FileNameToOpen = "False" Then
MsgBox "Closing"
Else
MsgBox "Opening " & FileNameToOpen
End If

End Sub

Zuccj
06-23-2017, 10:02 AM
I have the selecteditems line in now, any idea why I'd still be getting the file not found error? The path that pops up in the msgbox is exactly right, obviously as I'm clicking on the file, but I'm still getting an error saying file now found.

EDIT: found it. Was trying to open fopen which didn't actually have the file location. Just had to add an extra variable and set it equal to the selectedItems and it worked.