Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

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

Mobile
vba
»
Insert a line break at the end of a line and then insert text or a table
»
Word: insert characters at the end of a line
»
Text input fields in Word with vba c #
»
Create input fields in Word. C# ContentControls
»
Protect Word programmatically
»
Word vba: find current sentence
»
Word vba: Change document property title dynamically with Textbox Control
»
Word Code: Edit photos and controls in vba
»
Word: Find vba Code Example
»
Word vba macro: Changing WordArt with glow effect and shadow

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de