Title: Automatically change the extension of the filename in save dialog
Question: How to get the extension of the filename to change when the filetype is changed in a save dialog box.
Answer:
Many products have this feature on their save dialogs, e.g. in Excel if the filename in the Save dialog is showing as "Book1.xls" and you change the File Type to ".txt" then filename is automatically changed to "Book1.txt"
This example uses an standard FileSaveAs Action with an embedded dialog.
First add a reference to CommDlg.
Uses
...., CommDlg;
This code is attached to the change file type event which fires when the user changes the extension combobox.
Then extract the new extension from the filter list, change the filename and send it back to the dialog box.
procedure TForm1.FileSaveAs1SaveDialogTypeChange(Sender: TObject);
var
I : Integer;
Fn, Ext : String;
ExtList : TStringList;
const
CB_FILENAME_ID = 1148;
begin
ExtList:=TStringList.Create;
try
ExtList.Delimiter:='|';
ExtList.DelimitedText:=FileSaveAs1.Dialog.Filter;
Ext:=ExtList.Strings[FileSaveAs1.Dialog.FilterIndex*2-1];
finally
ExtList.Free;
end;
I:=Pos(Ext,';');
if I0 then Ext:=Copy(Ext,1,I-1);
Ext:=StringReplace(Ext,'*','',[rfReplaceAll]);
Fn:=ChangeFileExt(ExtractFilename(FileSaveAs1.Dialog.FileName),Ext);
SendMessage( GetParent(FileSaveAs1.Dialog.Handle), CDM_SETCONTROLTEXT, CB_FILENAME_ID, LongInt(Pchar(Fn)));
end;