Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Protect Word programmatically

02.11.2018 (๐Ÿ‘61)

Word protection program in vba .protect

 

Protect Word documents at runtime. Here in vba / C # interop, macro

 

Protect document

For protection, you can reduce the input to input fields with WdProtectionType.wdAllowOnlyFormFields

     string sPassword = Settings1.Default.Password;

     //-< protect word >-

     _doc.Protect( Word.WdProtectionType.wdAllowOnlyFormFields,Password:sPassword);

     //-</ protect word >-

 

Remove document protection

     //-< unprotect word >-

     string sPassword = Settings1.Default.Password;

     _doc.Unprotect(sPassword);

     //-</ unprotect word >-

 

 

 

 

 

Example code

In C # VSTO Interop

Correspondingly in vba without semicolon

private void replace_Serial_Fields()

 {

     //-----------------< replace_Serial_Fields() >-----------------

     //-< unprotect word >-

     string sPassword = Settings1.Default.Password;

     _doc.Unprotect(sPassword);

     //-</ unprotect word >-

 

     //*Serienbrieffelder=ActiveDocument.FormFields[]

     //--< @Loop: SerialFields >--

     foreach (object varField in _doc.MailMerge.Fields)

     {

         //ยซKDNRยป

         Word.MailMergeField field = (Word.MailMergeField)varField;

         string sField_Content = field.Code.Text;

         int posField = sField_Content.IndexOf("MERGEFIELD ");

         if (posField > -1) // MERGEFIELD SANr

         {

             //---< IsMail_Field >---

             //< get fieldname >

             posField = posField + "MERGEFIELD ".Length;

             string sField = sField_Content.Substring(posField);

             sField = sField.Trim();

             //</ get fieldname >

 

             //< get grid >

             int intColumn = get_Column_with_Header(sField);

             //</ get grid >

 

             if (intColumn >= 0)

             {

                 DataRowView row = (DataRowView) _dataGrid.SelectedItem;

                 string sReplace = row.Row.ItemArray[intColumn].ToString();

 

                 //< replace field >

                 field.Select();

                 _app.Selection.Text = sReplace;

                 //</ replace field >

             }

             //---</ IsMail_Field >---

         }

     }

     //--</ @Loop: SerialFields >--

 

 

     //-< unprotect word >-

     _doc.Protect( Word.WdProtectionType.wdAllowOnlyFormFields,Password:sPassword);

     //-</ unprotect word >-

     //-----------------</ replace_Serial_Fields() >-----------------

 }

 

 

 

 

 

programmatically