none
Event on editing / Prevent editing RRS feed

  • Question

  • Dear All!

    I would like to create a Word AddIn and I am missing an event from Word.Application or Word.Document something like:

    ...
    Doc.OnEditing+=new EditingHandler(Doc_OnEditing);
    ...
    
    bool Doc_OnEditing(Document Doc)
    {
    if ( Condition == true )
      return true; // Allow user to change anything, typing, formatting, etc. 
    else
      MessageBox.Show("Document cannot be edited, because ...");
      return false;
    }

    Do you have any idea how to implement this behavior in Word 2007/2010 or is it possible to do it at all?

    Thank you!

    Thursday, April 19, 2012 5:02 PM

Answers

  • This, maybe?

    ActiveDocument.Protect wdAllowOnlyReading, , "Impossibletoguesspassword"

    Friday, April 20, 2012 2:30 PM
  • You can close the document without saving, so it will retain the original settings.  Anyways, there wouldn't be anything to save, if the document is opened in read only mode, right?
    Friday, April 20, 2012 3:51 PM
  • Let say you find a way to do this, said user might have a bigger problem understanding why he cannot edit a document HE protected himself... I know I would be frustrated!

    That being said, if the document is already protected, then why not leave it like that?

    By the way, the "ActiveDocument.Saved" property is read/write. So if your code makes changes you do not want Word to save, just set "ActiveDocument.Saved" to True at the end of your code.

    If you must absolutely do this against all possible document settings, I suggest you do this with Windows API calls in order to change the file property to read only. You might have better luck in a Windows forum if you need help on this...

    Friday, April 20, 2012 4:38 PM

All replies

  • With Word 2000 and up there is an event called WindowSelectionChange. Wouldn't that do the trick?

    You can try this page for more general info on Events in Word:

    Word MVP's site - Application Events

    Friday, April 20, 2012 12:39 PM
  • That event is not fired when typing or formatting, but on cursor movements. So unfortunately from my point of view it is useless.
    Friday, April 20, 2012 12:51 PM
  • It depends on your overall goal.

    Above, you seem to only be interested in allowing a user to edit certain part of the document. The event I suggested is one way of detecting movement through the document, but depending on what you need, indeed, it may not be enough.

    Here is the list of all available events:

    http://msdn.microsoft.com/en-us/library/bb288738%28v=office.12%29.aspx

    Also, see if this can help...

    Word MVP's - Restricting editing in header.

    Friday, April 20, 2012 1:41 PM
  • Thank you for your help!

    I saw a document management system some months ago (do not remember its name) (maybe SharePoint behaves the same), which was able to prevent user changes while the document was checked in. I would like to know how they do this trick.

    Friday, April 20, 2012 2:00 PM
  • In that case, simply firing the Document_Open event and checking the environment maybe enough. If the environment states no changes, then flagging the document as read only or something like that may be enough.
    Friday, April 20, 2012 2:14 PM
  • But how is it possible to set the document read only?

    the ...Word.Document.ReadOnly property is read only, and the ...Word.Document.ReadOnlyRecommended cannot be used for this purpose.

    Friday, April 20, 2012 2:21 PM
  • This, maybe?

    ActiveDocument.Protect wdAllowOnlyReading, , "Impossibletoguesspassword"

    Friday, April 20, 2012 2:30 PM
  • Good idea, but if that particular document had protection by user then the addin overwrites it...
    Friday, April 20, 2012 3:32 PM
  • You can close the document without saving, so it will retain the original settings.  Anyways, there wouldn't be anything to save, if the document is opened in read only mode, right?
    Friday, April 20, 2012 3:51 PM
  • You are right, but imagine if a document is protected by a user. At open event the addin should unprotect first or can ask the user to do it. After that the addin protects it. Hmmm. No. And the user does not understands why he had to unprotect the document, because it is protected again.

    And these calls make changes to the document...

    Anyway, it would be a solution if all the documents were unprotected.

    Friday, April 20, 2012 4:17 PM
  • Let say you find a way to do this, said user might have a bigger problem understanding why he cannot edit a document HE protected himself... I know I would be frustrated!

    That being said, if the document is already protected, then why not leave it like that?

    By the way, the "ActiveDocument.Saved" property is read/write. So if your code makes changes you do not want Word to save, just set "ActiveDocument.Saved" to True at the end of your code.

    If you must absolutely do this against all possible document settings, I suggest you do this with Windows API calls in order to change the file property to read only. You might have better luck in a Windows forum if you need help on this...

    Friday, April 20, 2012 4:38 PM