I'm using automation to open documents in Word. Sometimes I need to open document in Read mode ON:
var
WordDocument: _Document;
WA: TWordApplication;
begin
WA := TWordApplication.Create( nil );
WA.OnQuit := DocumentClose;
WA.Connect;
WordDocument := Wa.Documents.Open( FileName, EmptyParam, true {ReadOnly}, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam );
But user can off the Read mode in opened document:
How can I handle this in OnQuit
event in procedure DocumentClose
?
In DocumentClose
I want to know if document is in read mode or not.
I do not have any solution because I did not have enough experience with it. So, I need your suggestions, advices about it. Sorry for my English and if I have to add more informations, please let me know. Thanks
UPDATE
I've tried to read protection type, but it's always return the first case. So, when document opening as ReadOnly isn't protected as wdAllowOnlyReading. Some documents can be protected with password, but there is not problem with it.
const
wdAllowOnlyReading: Longword = $00000003;
wdNoProtection: Longword = $ffffffff;
var
ProtectionType: TOleEnum;
begin
ProtectionType := WordDocument.ProtectionType;
case ProtectionType of
wdNoProtection : Showmessage('NoProtection');
wdAllowOnlyReading: Showmessage('ReadOnly');
end;
end;
I'm not sure exactly what you mean by "ReadOnly".
The WordDocument has a ReadOnly
boolean property which is read-only in the sense that you can read its value but not set it. This property returns true if when the document was opened, it was already open e.g. on a different workstation, so that the user would get the prompt "This document is locked for editing ..." and asked whether to open the document in read-only mode or whether Word should open a copy instead.
The other sense in which a Word document might be "read only" is if the used has marked it "Final" by clicking the Word button (that leads to the File menu, etc) and gone to Prepare | Mark as Final
(in the "Ribbon" versions of MS Word).
To read these properties in code you can do e.g.
if WordDoc.Final then
Caption := 'Final'
else
Caption := 'not Final';
if WordDoc.ReadOnly then
Caption := Caption + ' Read only'
else
Caption := Caption + ' Read/write'
Note: The Final
property is not surfaced in Delphi's Word2000.Pas, so to use it you need to go from early binding to late binding, like this:
var vWordDoc : OleVariant;
[...]
vWordDoc := WordDoc;
if vWordDoc.Final then
[...]
Unlike the ReadOnly
property, you can toggle the Final
property simply by
WordDoc.Final := not WordDoc.Final
But whether you can do this successfully when WordDoc.ReadOnly
is True
depends on why WordDoc.ReadOnly
is True
.
If WordDoc.ReadOnly
is True
because the document was edit-locked when it was opened because it was already open at another workstation, WordDoc.Final
is read-only. Otoh, if it's True
because you specified ReadOnly
in the call to .Open(), then you need to watch out: You can then set Final
to False
and the user will then be able to edit the document despite its having been opened ReadOnly
!
Another complication is that ProtectionType
is not directly related to "ReadOnly", as I imagine you've gathered: it can, but doesn't necessarily, prevent editing except to certain regions of a document.