Page 1 of 1

TRichViewEdit - Opening an RTF File

Posted: Fri Feb 08, 2013 11:19 am
by Graham
If I have an RTF file open in Word and then try to open it with RichViewEdit, it gives an error. The modification below fixes the problem, but I can't see a way of letting the caller know that the file is now open for "Read Only". I have done a bodge to call the standard "OpenFile" API to determine what is going to happen before I call the RTF read function from RichViewEdit.

function TRVRTFReader.ReadFromFile(const AFileName: String): TRVRTFErrorCode;
var Stream: TFileStream;
begin
Result := rtf_ec_FileOpenError;
try
Stream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);

except
try
Stream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
except
Stream := nil;
end;
end;
if Stream<>nil then begin
Result := ReadFromStream(Stream);
Stream.Free;
end;
end;

Posted: Fri Feb 08, 2013 7:07 pm
by Sergey Tkachenko
I think it's not a good idea to use fmShareDenyNone. What if the file will be modified while reading?

Posted: Mon Feb 11, 2013 9:31 am
by Graham
I take your point, but I would have thought it would be better to warn the user that the file is already open by another process and then give the user the choice of opening the file as ReadOnly. The RichEdit application would then force a SaveAs when the user tries to save any changes they may make.