Consulting

Results 1 to 3 of 3

Thread: How to save a user selected file as a variable?

  1. #1
    VBAX Regular
    Joined
    Jun 2017
    Posts
    23
    Location

    How to save a user selected file as a variable?

    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.
    Last edited by Zuccj; 06-23-2017 at 09:19 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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
    Last edited by Paul_Hossler; 06-23-2017 at 09:49 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jun 2017
    Posts
    23
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •