How to change file extensions in VBA

Refresh

April 2019

Views

15.9k time

1

I feel like this must be simple, but I can't find the answer. I'm saving a bunch of csv files using vba and would like to change all the file extensions from .csv to .txt to import into another program (Revit) which only recognizes the .txt extension. Is this possible? Here is the command I'm using.

    For I = 1 To WS_Count

        path = CurDir() + "\" + ActiveWorkbook.Worksheets(I).Name

        Sheets(ActiveWorkbook.Worksheets(I).Name).Select
        ActiveWorkbook.SaveAs Filename:=path, FileFormat:=xlCSV, CreateBackup:=False

        Name path As ("path" + ".txt")

     Next I

Thanks!

3 answers

3

You don't even need to open the files to rename them.

Sub M_snb()
  name "G:\OF\example.csv" As "G:\OF\example.txt"
end sub
snb
1

You should change

FileFormat:=xlCSV 

to

FileFormat:=xlTextWindows

See

https://msdn.microsoft.com/en-us/library/office/ff198017.aspx

or

The xlFileFormat enumeration (Excel) on MSDN

0

Ok got it. You can just ad txt to the file name, even if it is in the CSV format.

WS_Count = ActiveWorkbook.Worksheets.Count
         For I = 1 To WS_Count

        path = CurDir() + "\" + ActiveWorkbook.Worksheets(I).Name + ".txt"

        Sheets(ActiveWorkbook.Worksheets(I).Name).Select
        ActiveWorkbook.SaveAs Filename:=path, FileFormat:=xlCSV, CreateBackup:=False


        Debug.Print (test)
        Debug.Print (path)

     Next I