I have a macro that creates a new workbook, copies some data from the source book, pastes into the newly created book, then saves that new book and closes it. This code works PERFECTLY in Excel 2013, but does not function in Excel 2007. It breaks on the line indicated in my code below with the 1004 error: Method 'SaveAs' of object '_Workbook' failed
The code in question:
'File Save As Dialog
Dim FileSaveName As Variant
FileSaveName = Application.GetSaveAsFilename(InitialFileName:=SavePath & InitFileName, FileFilter:="CSV (Comma Delimited) (*.csv), *.csv, Text (Tab Delimited) (*.txt), *.txt", Title:="Save Results As")
If FileSaveName = False Then
MsgBox "Operation Cancelled", vbOKOnly
Exit Sub
ElseIf Dir(FileSaveName) <> "" Then
MsgBox "File already exists! Please choose a different name and try again.", vbCritical
Exit Sub
Else
'Create new workbook
Dim ResultsBook As Workbook
Set ResultsBook = Workbooks.Add
'
'
'
'copy some stuff from active workbook and paste into newly created workbook
'
'
'
'File Save
Dim FileSaveType As String
Dim FileSaveFormat As Long
FileSaveType = Right(FileSaveName, Len(FileSaveName) - InStr(FileSaveName, "."))
If FileSaveType = "csv" Then
FileSaveFormat = xlCSV
ElseIf FileSaveType = "txt" Then
FileSaveFormat = xlText
End If
ResultsBook.SaveAs FileName:=FileSaveName, FileFormat:=FileSaveFormat 'code breaks on this line
ResultsBook.Saved = True
ResultsBook.Close
My guess is that it has to do with the fact that I have a variable file format, but this is required given that the user needs to be able to save as either a CSV or tab delimited text, depending on the application.
Like I said, this code runs perfectly in Excel 2013. Some computers at my place of business however use older versions of office, 2007 being common, and this doesn't want to function on those older versions. Any idea how I might be able to modify this code to enable it to run on older versions of Excel (at least as old as 2007) as well?
Many thanks!
Bookmarks