Have a look at the following code.
Since Application.ActiveDocument.ReadOnly
is not writable, I use Application.ActiveDocument.Final
which works great.
"Returns or sets a Boolean that indicates whether a document is final. Read/write." http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15).aspx
It is very IMPORTANT that you call this after OleClientSite.doVerb()
, otherwise Application.ActiveDocument
is not initialized and nothing happens.
/**
* Sets a boolean that indicates whether a document is final (read only)<br/>
* http://msdn.microsoft.com/en-us/library/office/ff838930(v=office.15).aspx<br/>
* <br/>
* IMPORTANT: Call after OleClientSite.doVerb(), otherwise Application.ActiveDocument is not initialized
*
* @param clientSite
* @param readOnly
*/
public static void setFinal(OleClientSite clientSite, boolean readOnly) {
OleAutomation oleAutomation = new OleAutomation(clientSite);
int[] ids = oleAutomation.getIDsOfNames(new String[] { "Application" }); //$NON-NLS-1$
if (ids != null) {
Variant variant = oleAutomation.getProperty(ids[0]);
if (variant != null) {
OleAutomation application = variant.getAutomation();
ids = application.getIDsOfNames(new String[] { "ActiveDocument" }); //$NON-NLS-1$
if (ids != null) {
variant = application.getProperty(ids[0]);
if (variant != null) {
OleAutomation activeDocument = variant.getAutomation();
ids = activeDocument.getIDsOfNames(new String[] { "Final" }); //$NON-NLS-1$
if (ids != null) {
activeDocument.setProperty(ids[0], new Variant(readOnly));
}
activeDocument.dispose();
}
}
application.dispose();
}
}
oleAutomation.dispose();
}