Denying or detecting document modifications

T

Timo Partanen

Hi,

In Word 2007, is it possible to open a Word document as a read-only document
and programmatically (from add-in) protect the document from changes in
similar fashion like opened from SharePoint in read-only mode? Setting edit
restrictions from Word's "Protect document" menu looks quite similar
behavior but it is still not the same (the user can unprotect the document
etc.).

Is it possible to detect document modifications? Is there any event etc.
that would trigger when the document gets modified?

Best regards,
 
A

Alan Moseley

As you are already accessing the document from your add-in, can you not just
programatically unprotect it, make your modifications and then
programatically protect it again after?
 
J

\Ji Zhou [MSFT]\

Hello Timo,

As far as I know, there is no event that triggers when the document gets
modified. And I am still not very clear about what the exact objective you
want to achieve. Currently, I have the following two assumptions. Would you
mind clarifying it, and then we can have a better discussion.

1.Open the document as read-only.

In this case, the document can be edited, but when we try to save the
document, the Word will ask us to save the document as another one. That is
to say, the original document does not change. We can achieve this by
setting the Documents.Open()'s third parameter ReadOnly to true. Note this
argument doesn't override the read-only recommended setting on a saved
document. For example, if a document has been saved with read-only
recommended turned on, setting the ReadOnly argument to False will not
cause the file to be opened as read/write.

You can get a detailed information in this MSDN document,
http://msdn.microsoft.com/en-us/library/bb216319.aspx

2.Protect the document so that the end user cannot edit it.

Actually, when we call Document.Protect, we can pass a string type
parameter as the password. If someone wants to unprotect it later, he must
know the password. Consequently, we do not need to be worry about the end
user will unprotect it by themselves. The VB version Code looks like,

ActiveDocument.Protect(Type:=wdAllowOnlyReading, NoReset:=False,
Password:="851230", UseIRM:=False, EnforceStyleLock:=False)

Please let me know if the above resolves your issue. We are looking forward
to your reply.

Have a nice day, Timo!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Timo Partanen

Hi Ji,

Thanks for the quick response. We are trying to address an issue in a
specific environment. Document files are opened from location which is by
default a read-only storage. If the user wishes to modify document files, an
action must be taken elsewhere to make them modifiable. One could consider
this as a revision control system with file locking mechanism.

Users tend to open documents in read-only mode, and begin to edit them
without noticing that the file cannot be saved. We would like to make it
clear that the user must not try to edit the read-only document.

I'm not very keen to use the document's protection method, because it
actually affects to document's contents. The document protection setting may
be already used in other means, and we wouldn't like to make modifications
to the document itself. Besides, if we protect the document with a password,
the user is prompted for password on first modification attempt, which may
be bit confusing.

Best regards,
 
T

Timo Partanen

Hi Alan,

And thanks for the response. Actually we are trying to avoid the document's
"Protect" method and other modifications to the document itself, but rather
seek if the behavior of the Word application could be altered to prevent all
modifications? Thanks anyway, please see my response for Ji for more
details.

Best regards,
 
C

Cindy M.

Hi Timo,
Actually we are trying to avoid the document's
"Protect" method and other modifications to the document itself, but rather
seek if the behavior of the Word application could be altered to prevent all
modifications?
You have to realize that Word was not designed with this in mind. Using some
kind of protection will be your only option. What you appear to need is a
"Viewer". Microsoft has released "Viewers" for some versions of Word, but I'm
not sure about 2007. But if there is one, then the best option would be for
your software to open the documents in the Viewer, rather than in Word. Or
possibly in a browser window.

Something else to consider, if this is more a matter of convenience than
protection, would be whether you could force Word to open the files in the
"Reading view". (This is what you get if you open a Word document from Outlook,
for example.) In this view, editing is not enabled by default, although it can
be turned on. Depending on the document content things might look a bit odd...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

\Ji Zhou [MSFT]\

Hello Timo,

I agree with Cindy¡¯s comments. Based on my experience, I am not aware of
any event which fires when the document gets modified. So, the Protect()
should be the closest approach to your demand.

If that is really not the option you want to take, you may consider the
Reading View as Cindy mentioned. I write the following codes and test all
right against Word 2007.

public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
applicationObject = application as Word.Application ;
addInInstance = addInInst;
applicationObject.ActiveWindow.View.ReadingLayout = true;
applicationObject.WindowSelectionChange += new
Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(applicationObject_WindowSelectionChange);
}

void
applicationObject_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection
Sel)
{
if (!applicationObject.ActiveWindow.View.ReadingLayout)
{
System.Windows.Forms.MessageBox.Show("This document is not supposed to
be changed");
applicationObject.ActiveWindow.View.ReadingLayout = true;
}
}

This is just a tricky workaround and it works because the
WindowSelectionChange event fires when the window change to normal view from
the reading view. Thus we can prevent the user from closing the reading view
using the above codes. But this behavior is not documented and no guarantee
is made that it will not be changed in future version.

Please let me know if you have any future questions or concerns on this, I
am happy to be of any future help. Have a nice day!


Best regards,
Ji Zhou ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Timo Partanen

Hi Cindy,

Yes, we understand this may not be possible. However in Word 2007, when used
with SharePoint server, has quite similar behavior already implemented. It
is still likely that this functionality is not exposed in the public API.

Utilizing the reading layout would be a user-friendly solution anyway and
definitely worth of implementation.

Thanks and Regards,
 
T

Timo Partanen

Hi Ji,

The reading view trick would be definitely an improvement and worth of
consideration for us.

However I still believe that Word 2007 already has a kind of “enforced
read-only” mode implemented, it shows up when the document is opened from
SharePoint server. Let’s hope this will be made available in to the public
API too in future releases… It definitely should be available to other
developers, too.

Thanks again for the help.

Best regards,
 
C

Cindy M.

Hi Timo,
However I still believe that Word 2007 already has a kind of “enforced
read-only” mode implemented, it shows up when the document is opened from
SharePoint server. Let’s hope this will be made available in to the public
API too in future releases… It definitely should be available to other
developers, too.
You should check with the SharePoint folks about this. Word will respect the
Read-Only attribute applied at the FILE and FOLDER level and I suspect that's
what may be happening in this case. This is outside the Word object model.

But I suppose that would be a possibility, if your Add-in can intercept the
user request *before* the file starts opening and set the file attribute,
then open the file. How to best set the attribute depends on which technology
(programming language) you're using. VBA, for example, provides the SetAttr
method of the FileSystem object.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top