Select a file by user by VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FloridaBlueJay
    New Member
    • Jul 2016
    • 9

    Select a file by user by VBA

    I am looking for a efficient way for a user to select a file and insert that selection into a piece of common code:
    Code:
    Name "C:\Folder\Selected file" As "C:\Folder2\Selected file new format"
    I have files that I must change the extension type to be able to import into Access 2013. I have build code that does the import, process and insert into the data base tables. The Name function works but it is currently hard coded to test the entire process which works well.
    I hope this makes sense. This is the final piece to complete a project.
    Thanks in advance.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    1. Set a Reference to the Microsoft Office XX.X Object Library.
    2. Copy-N-Paste the following Code which will:
      1. Open the Office File Dialog.
      2. Allow you to Select a File.
      3. Move that File to the C:\Folder2\ Folder, keep its Base Name, change its Extension to '.xxx'.
      4. An Example would be:
        Code:
        C:\TestDir\Sub\MyFile.xls ==> C:\Folder2\MyFile.xxx

      Code:
      Dim strButtonCaption As String
      Dim strDialogTitle As String
      Dim strBaseFileName As String
      
      strButtonCaption = "Open"
      strDialogTitle = "Select File"
      
      With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False       'Critical Line
          .ButtonName = strButtonCaption
          .InitialFileName = CurrentProject.path & "\"
          .InitialView = msoFileDialogViewDetails     'Detailed View
          .Title = strDialogTitle
        If .Show Then
          strBaseFileName = Mid$(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") + 1)
            strBaseFileName = Left$(strBaseFileName, InStr(strBaseFileName, ".") - 1)
              Name CStr(.SelectedItems(1)) As "C:\Folder2\" & strBaseFileName & ".xxx"
        End If
      End With

    Comment

    • FloridaBlueJay
      New Member
      • Jul 2016
      • 9

      #3
      Thank you for your help. I am able to adapt your suggestion to my project. Merry Christmas.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Same to you, have a very, Merry Christmas!

        Comment

        Working...