Your code should work; you should check the error code because it will have meaningful info.
It could be a permissions (file) issue, file contention issue, bad folder (invalid characters), etc., existing file that is locked. Wouldn't hurt to see if the file/folder exists first. You can check for the file using FileExists(as_filename) or you can check a folder by DirectoryExists(as_directory).
You could try Excel8! for Excel version 8 or higher but I think your Excel! should work just fine.
// Add saveastype as parameter to function
public function integer save_dw_to_file (datawindow adw_datawindow, &
string as_filename, &
string as_folder, &
SaveAsType sat_SaveType)
int li_rc
string ls_tmp_file
ls_tmp_file = as_filename
// add file extension based on saveastype
choose case sat_SaveType
case Excel!, Excel5!, Excel8!
ls_tmp_file += '_temp.xls'
case PDF!
ls_tmp_file += '_temp.pdf'
end choose
if FileExists ( ls_tmp_file ) Then
if MessageBox('File already exists','Would you like to replace: ' + &
ls_tmp_file + '?', Question!, YesNo!, 2) = 2 then
return -1
end if
end if
// save type based on parameter to function
li_rc = adw_datawindow.saveas(ls_tmp_file, sat_SaveType, true)
if li_rc = -1 then
MessageBox('Error saving file','Unable to save file: ' + ls_tmp_file)
end if
return li_rc