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

2

I want to make a VBA code that opens a specific spreadsheet and saves it as tabbed text. I'm having this formulated code

 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
    
asked by anonymous 17.11.2014 / 19:29

1 answer

1

You were trying to save a string. You 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
    
17.11.2014 / 20:38