如何在 VBA 中更改文件扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28774686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:25:09  来源:igfitidea点击:

How to change file extensions in VBA

excelvbaexcel-vbacsv

提问by user1707675

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.

我觉得这一定很简单,但我找不到答案。我正在使用 vba 保存一堆 csv 文件,并希望将所有文件扩展名从 .csv 更改为 .txt 以导入另一个仅识别 .txt 扩展名的程序 (Revit)。这可能吗?这是我正在使用的命令。

    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!

谢谢!

回答by snb

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

回答by cronos2546

You should change

你应该改变

FileFormat:=xlCSV 

to

FileFormat:=xlTextWindows

See

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

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

or

或者

The xlFileFormat enumeration (Excel) on MSDN

MSDN 上的 xlFileFormat 枚举 (Excel)

回答by user1707675

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

好的,我知道了。您可以将 txt 添加到文件名中,即使它是 CSV 格式。

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