Choose a file in a folder and save it as text automatically

Asked

Viewed 962 times

2

I want to make a VBA code that opens a specific spreadsheet and save it as tabulated text. I am with this code formulated

 Sub gerararquivotxt()

Dim intChoice As Integer
Dim strPath As string

'permitir escolher só um arquivo
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'abrir a caixa de diálogo
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determinar a escolha
If intChoice <> 0 Then
    'pegar o caminho do arquivo selecionado
    strPath = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)
        End If
'exportar arquivo selecionado como texto
strPath.SaveAs Filename:= _
        "C:\Users\USER-NOTE\Downloads\PLANILHA_RECEBIMENTO_MENSAL.txt", _
        FileFormat:=xlText, CreateBackup:=False

End Sub
  • And what mistake do you get ?

  • for strPath.saveAs "Invalid qualifier"

1 answer

1


You were trying to Save a String.
Would need to Open the Workbook, Save it as, then Close it;

Sub GerarArquivoTXT()
    Dim intChoice As Integer
    Dim strPath As String

    'permitir escolher só um arquivo
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    'abrir a caixa de diálogo
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determinar a escolha
    If intChoice <> 0 Then
        'pegar o caminho do arquivo selecionado
        strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
    End If

    Dim wb As Workbook        
    Set wb = Workbooks.Open(strPath)
    wb.Application.DisplayAlerts = False

    'exportar arquivo selecionado como texto
    wb.SaveAs Filename:= _
            "C:\Users\USER-NOTE\Downloads\PLANILHA_RECEBIMENTO_MENSAL.txt", _
            FileFormat:=xlText, CreateBackup:=False,  AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
    wb.Close

End Sub
  • It worked perfect. It asks confirmation if you want to save even the file, has how to delete this step?

  • I found this answer, and merged it with the code above, see if it saves without asking again for confirmation. http://stackoverflow.com/a/14634781/194717

  • 1

    Perfect! It was totally worth it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.