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

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: