运行时错误1004

时间:2011-07-08 19:38:49

标签: vba excel-vba excel

尝试运行以下内容时出现以下错误:

  

“此扩展程序无法使用   选定的文件类型。改变   文件名文本中的文件扩展名   框或选择不同的文件类型   将保存更改为类型。“

代码:

Dim strPath As String
Dim strFolderPath As String


strFolderPath = "Y:\

strPath = strFolderPath & _
Sheet1.Range("A1").Value & _
Sheet1.Range("B1").Value & ".xlsx"


ActiveWorkbook.SaveAs Filename:=strPath

2 个答案:

答案 0 :(得分:8)

错误表示ActiveWorkbook正在尝试另存为“.xlsx”的文件格式。要强制它保存为.xlsx,您还必须传递fileformat。

ActiveWorkbook.SaveAs Filename:=strPath, FileFormat:=xlOpenXMLWorkbook

答案 1 :(得分:0)

当我尝试将启用宏的工作簿(xlsm)转换为普通工作簿(xlsx)时,我遇到了同样的问题!我终于放弃使用ActiveWorkbook.SaveAs方法并改为使用以下代码:

' Code from http://www.mrexcel.com/forum/excel-questions/516366-saving-xlsm-file-xlsx-using-visual-basic-applications.html#post4478019
sub saveAsXlsx
Dim mySheetList() As String
ReDim mySheetList(0 To (ThisWorkbook.Sheets.Count) - 1)
Dim a As Integer
a = 0
For Each ws In ActiveWorkbook.Worksheets
    mySheetList(a) = ws.Name
    a = a + 1
Next ws

'actually save
Worksheets(mySheetList).Copy
ActiveWorkbook.SaveAs fileName:=flenme 'default ext

代码最初来自here