Reputation: 23
Our PowerBuilder application generates a report by printing a DataWindow as PDF file. Now we would like to modify the PB such that an Excel is generated instead of PDF.
In my PB code I tried using the following function:
public function integer save_dw_to_file (datawindow adw_datawindow, string as_filename, string as_folder);
string ls_tmp_file_xls
ls_tmp_file_xls = as_filename+'_temp.xls'
adw_datawindow.saveas(ls_tmp_file_xls,Excel!,true)
return 1
end function
Note: adw_datawindow is the DataWindow that I want to print; as_filename is the output filename. However, this seems not work because I got an error when I open the file.
Do you know how to do this? Our environment:
PB Version: PB 12 Classic; Excel Version: MS Excel 2007
Upvotes: 2
Views: 10116
Reputation: 4174
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
Upvotes: 2