protected override void ReEncrypt(string sourceFile) { object doc = null; try { DocumentUpgradeWindowSuppressor ds = new DocumentUpgradeWindowSuppressor(); ds.StartMonitoring(FileType.WordDocument); doc = OfficeApplicationCache.Instance.OpenDocument(sourceFile, false, AttachmentFileType); if( m_typeProtect != Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection ) { Word12Interop.Document wordDoc = doc as Word12Interop.Document; object noReset = true; // only used by Word.WdProtectionType.wdAllowOnlyFormFields else ignored object password = DocumentProtectionPassword; wordDoc.Protect2002(m_typeProtect, ref noReset, ref password); Logger.LogDebug(string.Format("WordEncryption.ReEncrypt: Successfully re-applied protection to document \"{0}\"", sourceFile)); } // Modify document passwords, save to -another- temporary file (decryptedPath) // Need to save twice, or for some reason the read/modify passwords get kept Logger.LogDebug(string.Format("WordEncryption.ReEncrypt: Saving attachment to \"{0}\"", sourceFile)); OfficeApplicationCache.Instance.SetOpenPassword(doc, OpenPassword); OfficeApplicationCache.Instance.SetWritePassword(doc, ModifyPassword); OfficeApplicationCache.Instance.Save(doc); OfficeApplicationCache.Instance.SaveDocumentAs(doc, sourceFile, false, OpenPassword, ModifyPassword); ds.StopMonitoring(); } finally { if (doc != null) { OfficeApplicationCache.Instance.CloseDocument(doc, false); } } }
/// <summary> /// Re-encrypts an office file /// </summary> protected override void ReEncrypt(string sourceFile) { object doc = null; try { DocumentUpgradeWindowSuppressor ds = new DocumentUpgradeWindowSuppressor(); ds.StartMonitoring(FileType.ExcelSheet); doc = OfficeApplicationCache.Instance.OpenDocument(sourceFile, false, AttachmentFileType); if (ProtectionPasswords.ContainsKey("*workbook*") && !string.IsNullOrEmpty(ProtectionPasswords["*workbook*"])) { ((Workbook)doc).Protect(ProtectionPasswords["*workbook*"], true, IsWindowsProtected); } foreach (Worksheet sheet in ((Workbook)doc).Sheets) { if (ProtectionPasswords.ContainsKey(sheet.Name) && !string.IsNullOrEmpty(ProtectionPasswords[sheet.Name])) sheet.Protect(ProtectionPasswords[sheet.Name]); } OfficeApplicationCache.Instance.SaveDocumentAs(doc, sourceFile, false, OpenPassword, ModifyPassword); ds.StopMonitoring(); } finally { if (doc != null) { OfficeApplicationCache.Instance.CloseDocument(doc, false); } } }