2011-07-08 172 views
1

當試圖運行下面的我得到以下錯誤:運行時錯誤1004

"This extension can not be used with the selected file type. Change the file extension in the file name text box or select a different file type by changing the save as type."

代碼:

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 
+0

是,excel 2007 – john

+5

如果ActiveWorkbook包含宏,則需要使用「xlsm」擴展名和參數FileFormat:= XlFileFormat.xlOpenXMLWorkbookMacroEnabled進行SaveAs。 –

回答

5

的錯誤意味着ActiveWorkbook試圖保存爲不同的文件格式然後是「.xlsx」。要強制它保存爲.xlsx,您還必須通過fileformat。

ActiveWorkbook.SaveAs Filename:=strPath, FileFormat:=xlOpenXMLWorkbook 
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