In the default Codeunit 412 is a function that opens a open or a save dialog.  The problem with this function is that we do not know if the open, save or cancel button was pressed.  I used the code
[code htmlscript=”false”]FileName := CommonDialogMgt.OpenFile(”,FileName,1,CommonDialogMgt.GetFilterString(1),1);[/code]
where CommondialogMgt is Codeunit 412 to ask the user where to save the file.  If I passed a default file name to this function and the user simply approved the suggestion I would get the same result as if the user pressed cancel.  I wanted to change this and added a few lines to Codeunit 412.

I added a global function SetProperties and GetFileName and a local function OpenFileWithError.
[code htmlscript=”false”]SetProperties(WindowTitle : Text[50];DefaultFileName : Text[1024];DefaultFileType : ‘ ,Text,Excel,Word,Custom,Xml,Htm,Xsd,Xslt’;FilterS
GlobalWindowTitle := WindowTitle;
GlobalDefaultFileName := DefaultFileName;
GlobalDefaultFileType := DefaultFileType;
GlobalFilterString := FilterString;
GlobalAction := Action;

GetFileName() : Text[1024]

OpenFileWithError(WindowTitle : Text[50];DefaultFileName : Text[1024];DefaultFileType : ‘ ,Text,Excel,Word,Custom,Xml,Htm,Xsd,Xslt’;Fil
IF DefaultFileType = DefaultFileType::Custom THEN BEGIN
Filter := FilterString;
Filter := GetFilterString(DefaultFileType);

CommonDialogControl.MaxFileSize := 2048;
CommonDialogControl.FileName := DefaultFileName;
CommonDialogControl.DialogTitle := WindowTitle;
CommonDialogControl.Filter := Filter;
CommonDialogControl.InitDir := DefaultFileName;
CommonDialogControl.CancelError := TRUE;

IF Action = Action::Open THEN

I added a few global variables and a code to the onRun trigger
[code htmlscript=”false”]GlobalFileName :=
OpenFileWithError(GlobalWindowTitle,GlobalDefaultFileName,GlobalDefaultFileType,GlobalFilterString,GlobalAction);[/code]This means that I have to change the code
[code htmlscript=”false”]FileName := CommonDialogMgt.OpenFile(”,FileName,1,CommonDialogMgt.GetFilterString(1),1);[/code]to
[code htmlscript=”false”]CommonDialogMgt.SetProperties(”,FileName,1,CommonDialogMgt.GetFilterString(1),1);
IF CommonDialogMgt.RUN THEN
FileName := CommonDialogMgt.GetFileName
FileName := ”;[/code]
and I will get an empty file name if cancel is pressed.  Attached is the NAVW16.00.01 Codeunit for NAV 2009 R2.

Codeunit 412

2 thoughts on “Using Common Dialog Cancel button

  1. Hi, thank you, but How can I disable or Hide the cancel button ? This is to force the user to open the file . Thanks in advance.

    1. I don’t think that is possible, but you can message the user if cancel is selected and the reopen the dialog.

Leave a Reply

%d bloggers like this: