vba保存为CSV

时间:2018-04-17 15:26:17

标签: excel-vba csv export-to-csv vba excel

我找到了将单张纸张保存为CSV的代码。 但我收到错误: 运行时错误' 1004' 无法访问只读文档' MasterCallOneList.CSV'

如何解决?

该文档实际上是一个不存在的新文档,因此我不知道为什么它会说该文档是只读的。

Application.DisplayAlerts = False

Dim strFullName As String
strFullName = Application.Path + "\MasterOneCallList.CSV"
ThisWorkbook.Sheets("Combined").Copy
ActiveWorkbook.SaveAs Filename:=strFullName, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close

Application.DisplayAlerts = True

2 个答案:

答案 0 :(得分:3)

可能是由于application.path目录中的保护级别所致。

你试过吗

Application.DisplayAlerts = False

Dim strFullName As String
strFullName = ThisWorkbook.Path + "\MasterOneCallList.CSV"
ThisWorkbook.Sheets("Combined").Copy
ActiveWorkbook.SaveAs Filename:=strFullName, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close

Application.DisplayAlerts = True

答案 1 :(得分:0)

strFullName = Application.Path + "\MasterOneCallList.CSV"

这可能是错误,路径名将是ms_office excel文件夹。尝试使用实际的文件夹路径字符串,代码应该工作。如果它确实有效,你将不得不想办法获得一个实际的路径字符串。

    Sub Button1_Click()

    Dim strFullName As String

    Application.DisplayAlerts = False

    strFullName = "C:\Users\dmorrison\Downloads\TestKryztof" + "\MasterOneCallList.CSV"
    ThisWorkbook.Sheets("Combined").Copy
    ActiveWorkbook.SaveAs Filename:=strFullName, FileFormat:=xlCSV, CreateBackup:=True
    ActiveWorkbook.Close

    Application.DisplayAlerts = True
End Sub