Link to home
Start Free TrialLog in
Avatar of aztec
aztec

asked on

Programmatically shutting down Windows Notepad

My application creates a report file for the user to view in
Windows Notepad, by simply clicking on a speedbutton. However, sometimes some users forget to close down this file and run my program again - which will attempt to re-create this report file. When this happens, they of course get an I/O error window because this report file is already open in Windows Notepad.
  is there a way I can automatically close this file (..if it happens to be open) whenever the user re-runs my program? It would save them from crashing the program.

Thanks
  Shawn Halfpenny
  drumme59@sprint.ca
ASKER CERTIFIED SOLUTION
Avatar of viktornet
viktornet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Madshi
Madshi

aztec,

if you want to close the notepad use something like

(1) PostMessage(FindWindow('Notepad',nil),WM_CLOSE,0,0);

Regards, Madshi.
Avatar of aztec

ASKER

Yes, but I don't want to just close down Notepad - as the user could have other files open which he is working on. Ideally I would like to check if the specific .rpt file is open, THEN close it down. See what I mean? Is there a way to check if a certain file is open, and if it is, THEN close it?

Regards
   Shawn
var
  F : File;
begin
  try  
    AssignFile(F, 'C:\windows\desktop\test.txt');
    try
      Reset(f);
    except
      SendMessage(TheFileHandle, WM_CLOSE, 0, 0);
  finally
    CloseFile(F);
  end;
end;

Not sure if it works and if this is the way to do but you might want to try it out....

//Viktor
aztec,

you could look if your filename is in the notepad's title bar (GetWindowText).
If you want to check if the file is open, use an exception statement (see Viktor's last comment).

Regards, Madshi.
Avatar of aztec

ASKER

Thanks Viktor.

Regards,
   Shawn