0

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:

enter image description here

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;
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • I guess you could try temporarily setting the file as read-only just before opening it, but that seems like a sloppy work-around. – Jerry Dodge Oct 07 '15 at 00:16
  • Maybe [this link](http://stackoverflow.com/questions/12630878/word-vba-document-readonly-status-incorrectly-returns-false) has a solution. – Tom Brunberg Oct 07 '15 at 03:55
  • I thought you were automating. In which case how is user involved? – David Heffernan Oct 07 '15 at 03:57
  • @DavidHeffernan When user just opened ReadOnly document - action not needed, when user opened ReadOnly doc. and then choosed View-Edit Document in MSWord then I need to do some action in my code – Roman Marusyk Oct 07 '15 at 06:46
  • Why is the user involved? Aren't you automating? – David Heffernan Oct 07 '15 at 07:20
  • What is that screen-shot of, exactly? – MartynA Oct 07 '15 at 08:18
  • 1
    The user can do anything they like surely. It's their computer after all. They can just kill Word if they like. Close the doc before you are finished. Just ask the user to leave it alone. Or better, don't even show the doc to them – David Heffernan Oct 07 '15 at 11:37

1 Answers1

1

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.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Thanks for your answer. First: I mean "ReadOnly" is that document open as on screenshot – Roman Marusyk Oct 07 '15 at 08:17
  • a) What **exact** version of MS Word are you using and b) which interface unit (I mean the one which comes with Delphi that puts the MS Office servers on the Component Palette) are you Using in your project? – MartynA Oct 07 '15 at 08:24
  • a) client can has a different version of MSWord. b) unit Word2000 – Roman Marusyk Oct 07 '15 at 08:26
  • Ah, ok, Word2000 doesn't have the Final property but you can still use it (provided the user is using a later version of Word than the 2000 one). See Note section of my answer. – MartynA Oct 07 '15 at 08:49
  • The reason I was asking about where your screen shot came from was that it doesn't look like any version of Word I've used, but I haven't used them all. So, as a matter of interest, which version is it? – MartynA Oct 07 '15 at 08:50
  • Thanks a lot for your help. I'll try use Final. Now I use MSWord 2013 – Roman Marusyk Oct 07 '15 at 09:02
  • Which Delphi version do you get the error with? I use D7 as a sort of "lowest common denominator" for answering automation questions. – MartynA Oct 07 '15 at 09:23
  • Well, all the code in my answer compiles fine in XE7, including the "vWordDoc := WordDoc". Maybe your trying to assign from the variant to the WordDoc: If you do that you need "WordDoc := IDispatch(vWordDoc) as WordDocument;" – MartynA Oct 07 '15 at 10:11
  • So what **exact** statement give the "Incompatble types" error message and what are your declarations of the two variables? – MartynA Oct 07 '15 at 10:27