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