Implement Document.Protect and Document.Unprotect in Word JavaScript API
I'm developing an add-in using the Word JavaScript API, and I need the ability to work with protected Word documents programmatically. In VBA and VSTO, I was able to unprotect these documents, alter them, and then protect them again. Please implement the https://learn.microsoft.com/en-us/office/vba/api/Word.document.protect and https://learn.microsoft.com/en-us/office/vba/api/Word.Document.Unprotect methods in Word's JavaScript API so that I don't have to do this through VBA or VSTO. It looks like this was already done in Excel (see https://learn.microsoft.com/en-us/javascript/api/office-scripts/excelscript/excelscript.worksheetprotection?view=office-scripts).
8 Comments
- mkanorwalaCopper Contributor
We are using office.JS add-in and javascript apis of word.
Is it technically possible to achieve the following using Office.js in a Word add-in?
We want to add custom properties (such as document IDs and metadata) to Word documents opened via our add-in. Based on the presence of specific custom properties or keywords, we aim to disable Word's default 'Save,' 'Save As,' and 'Share' options to prevent users from saving or sharing the document locally.
Additionally, we would like to implement custom protection mechanisms through our add-in. Could you please confirm whether this functionality can be implemented and suggest best practices or limitations we should be aware of?
- DavideBonoBrass Contributor
HI
it is possible to save and read custom properties via Office.js :https://learn.microsoft.com/en-us/javascript/api/word/word.customproperty?view=word-js-preview
It is not possible to intercept the save event or disable the button, I created the idea to handle the save event:
It is not even possible to implement document protection
Follow this post or my idea :- mkanorwalaCopper Contributor
Hi DavideBono thanks for your reply. Yes i Achieved the saving and reading custom properties via office.JS. but i want to see if we can achieve it by writing any .net command or handler to handle this but via office.js in some way? Is there a work around?. Or the other part is I am using web-dav to open files on word from server using web-dav url. So based on if the document is a web-dav url or not can we do it?
- martin-koubaCopper Contributor
Working with the JavaScript API reminds me of working with VBScript. First it's like "hey that's cool" until you realize "**bleep** can't use it at all and have to reimplement with something else".
- DavideBonoBrass Contributor
I uploaded a similar ticket, i'm following this one too
- frank-hoffmanCopper Contributor
Sure Yun_Wang. The use case we have here is that we're dealing with a legal document that has protected content sections. The reason for this is so customers don't inadvertently alter other parts of the document that are used in downstream processing. The customers want to have the ability to remove one of the content controls from the document but can't unless we unprotect the document first. In the legacy system there was some VBA that would do this for them. I've seen some other parts of the code that also needed to unprotect the document for other reasons, though I haven't tracked down all the use cases there yet.
- Yun_Wang
Microsoft
frank-hoffman Thanks for submitting this requirement. Can you let me know your use case here?
- mrwilson1980Copper Contributor
This seems like a very basic function that is needed and is missing. Microsoft, please read this and take action.