Monday, August 11, 2014

Protect word file using C# : Word automation step by step

Security:
     Security is the Most heard and most important 'keyword' in IT industry, Everyone want their documents play a secure travel, should not attack by spam or any intruder. Really, security is the main concern in now world, we can make word file secure by using password protection.
word security


What is our aim:
       protect word file using C# word automation

What we need:
      C#, Interop libraries

What are the different security can be assigned to word file:
         There are multiple types security we can assign to word file
1. Read-only protection
2. Comment only protection
3. Track revision protection



Read Only protection:
It Allow read-only access to the document.

Comment only protection:
It allows Allow only comments to be added to the document.

Track revision protection:
It Allow only revisions to be made to existing content.

Lets code it, (Protect word file):
To protect word file programmatically we need to add reference of interop assemblies in our code, I have explained it in my previous article you can check here
checkout below code snippet to protect file as 'ReadOnly'
 
         object objMiss = System.Reflection.Missing.Value;  
         object fileToOpen = Application.StartupPath + "\\test.doc";  
         object szPassword = "Pass";  
         object bFalse = false;  
         object bTrue = true;  
         //Start Word and create a new document.  
         objApp = new Word.Application();  
         objDoc = objApp.Documents.Open(ref fileToOpen, ref objMiss, ref objMiss, ref objMiss, ref objMiss,  
                         ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss,  
                         ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);  
         if (objDoc.ProtectionType == Word.WdProtectionType.wdNoProtection)  
         {  
           objDoc.Protect(Word.WdProtectionType.wdAllowOnlyReading, ref bFalse, ref szPassword, ref bFalse, ref bTrue);  
           objDoc.Save();  
           MessageBox.Show("Word document Protected successfully (for Read only)!", "Word Protect", MessageBoxButtons.OK, MessageBoxIcon.Information);  
         }  
         else  
         {  
           MessageBox.Show("Word document is already protected !", "Word Protect", MessageBoxButtons.OK, MessageBoxIcon.Information);  
         }  

In above code snippet we have used method  Protect of document class and pass 'wdAllowOnlyReading' enumeration and just save document with password. it is enough to accomplish the task. so simple.
like wise we can protect document with  'Comment ' and 'TrackRevision' protection

Unprotect word file:
Now after protection we can unprotect word file using Unprotect method of document class
see below snippet
 
         object objMiss = System.Reflection.Missing.Value;  
         object fileToOpen = Application.StartupPath + "\\test.doc";  
         object szPassword = "Pass";  
         object bFalse = false;  
         object bTrue = true;  
         //Start Word and create a new document.  
         objApp = new Word.Application();  
         objDoc = objApp.Documents.Open(ref fileToOpen, ref objMiss, ref objMiss, ref objMiss, ref objMiss,  
                         ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss,  
                         ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);  
         if (objDoc.ProtectionType != Word.WdProtectionType.wdNoProtection)  
         {  
           objDoc.Unprotect(ref szPassword);  
           objDoc.Save();  
         }  

So, with the above example we can say it is easy to protect and unprotect document programmatically.
You can get full source code here

Note:
    As we know word is heavy object so do not forget to close and dispose it, I have explained it in my previous blog

Summary:
Someone said, there is always scope for improvement. so I think its a on going think for programming too. Finally we have ready with our code, it is very simple to protect a file with customized password, still many things to discover with word and C#, we can them one by one in near future till then enjoy this stuff and be happy with C# and Word

Suggestions and comments/doubts are always welcome

Happy Programming
- Prasad