2

My goal is to export excel file to txt file format. The idea is to has a GUI to let user select the excel file that she/he wish to export and she/he can decide which file path and file name to save. Once users has finish input and output setting, he/she just need to click Export text button to export the excel file to txt file and save in the location that he/she has decided. The GUI as below

enter image description here

I've a macro to convert excel file to txt format

Private Sub ConvertToText()
ActiveWorkbook.SaveAs FileName:="C:\Projects\ExelToText\Text.txt", FileFormat:=xlCurrentPlatformText, CreateBackup:=False
End Sub

My question is how could I pass value from FileInput and FileOutput as variable to above macro instead harcode the filepath. Appreciate your helps and if you have any better suggestions, please share it out. Thanks

Below is the full source code

Private Sub ReadButton_Click()
OpenWorkbookUsingFileDialog
End Sub
------------------------------
Private Sub WriteButton_Click()
WriteWorkbookUsingFileDialog
End Sub
------------------------------
Private Sub ExportButton_Click()
ConvertToText
End Sub
------------------------------
Private Sub OpenWorkbookUsingFileDialog()

Dim fdl As FileDialog
Dim FileName As String
Dim FileChosen As Integer

Set fdl = Application.FileDialog(msoFileDialogFilePicker)

fdl.Title = "Please Select a Excel File"
fdl.InitialFileName = "c:\"
fdl.InitialView = msoFileDialogViewSmallIcons

fdl.Filters.Clear
fdl.Filters.Add "Excel Files", "*.xlsx; *.xls"

FileChosen = fdl.Show

If FileChosen <> -1 Then

MsgBox "You have choosen nothing"
ReadTextBox = Null
Else

 MsgBox fdl.SelectedItems(1)
 FileName = fdl.SelectedItems(1)
 ReadTextBox = FileName
End If

End Sub
-----------------------------------
Private Sub WriteWorkbookUsingFileDialog()

Dim file_name As Variant


file_name = Application.GetSaveAsFilename( _
    FileFilter:="Text Files,*.txt,All Files,*.*", _
    Title:="Save As File Name")


If file_name = False Then Exit Sub


If LCase$(Right$(file_name, 4)) <> ".txt" Then
    file_name = file_name & ".txt"
End If
WriteTextBox = file_name

End Sub
----------------------------
Private Sub ConvertToText()
ActiveWorkbook.SaveAs FileName:="C:\Projects\ExelToText\Text.txt",FileFormat:=xlCurrentPlatformText, CreateBackup:=False
End Sub
user1902849
  • 1,113
  • 6
  • 23
  • 33

1 Answers1

1

Make it so your subroutine ConvertToText requires a file path/string argument:

Private Sub ConvertToText(sourcePath as String, destPath as String)
    Dim wb as Workbook
    Set wb = Workbooks.Open(sourcePath)
    wb.SaveAs FileName:=destPath,       
    FileFormat:=xlCurrentPlatformText, CreateBackup:=False
    wb.Close
End Sub

Then, make small modifications to your ExportButton to send this parameter to the ConvertToText sub:

Private Sub ExportButton_Click()
    On Error Resume Next
    ConvertToText Me.TextBox1.Value, Me.TextBox2.Value 'Modify this so that it refers to the TextBoxes on your form
    If Err.Number <> 0 Then 
        MsgBox "Unable to convert file. Please ensure a valid file was entered.", vbCritical
    End If
    On Error GoTo 0
End Sub
David Zemens
  • 53,033
  • 11
  • 81
  • 130