Get FileNames with Path from User

HOW SHOULD I USE THE FOLLOWING CODE

Option Explicit

Dim outfile       As String
Dim Num           As Integer
Dim dlg           As FileDialog
Dim nFile         As Integer

Sub Get_FileNames()
OneMoreTime:
  nFile = FreeFile
  If ThisWorkbook.Path = "" Then
    MsgBox "PLEASE SAVE THIS WORKBOOK BEFORE EXECUTING THIS PROCEDURE"
    Exit Sub
  End If

  outfile = ThisWorkbook.Path & "\" & "Files_" & Format(Now(), "hh_nn_ss") & ".txt"
  Open outfile For Output As #nFile
  Set dlg = Application.FileDialog(msoFileDialogFilePicker)

  Dim selItem As Variant
  With dlg
    If .Show = -1 Then
      For Each selItem In .SelectedItems
        Write #nFile, selItem
      Next selItem
    Else
    End If
  End With

  Close #nFile

  Set dlg = Nothing
  Dim result
  result = MsgBox("A textfile with selected " & _
        "file name with path is saved at : " & _
          vbNewLine & outfile & vbNewLine & _
          "Would you like to save some more file names " & _
          "in a text file", vbOKCancel + vbQuestion, "Selva V Pasupathy")
  If result = vbOK Then
    GoTo OneMoreTime
  End If
End Sub

Leave a Comment

You must be logged in to post a comment.