首页 文章

C#以段落或范围级别以编程方式保护word文档

提问于
浏览
0

我的工具将处理超过1000个文档 . 我们在文档级别设置了Readonly,这导致了严重的性能问题 .
_appObject = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document _DocObj; string file = @“c:\ Users \ Public \ Public Documents \ Word12.docx”;
_DocObj = _appObject.Documents.Open(ref file,ref missing,ref missing,ref missing,
ref遗失,ref遗失,ref遗失,ref遗失,ref遗失,ref遗失,ref
失踪,参考缺失,参考缺失,参考缺失,参考缺失); //保护
appObject.ActiveDocument.Protect(Microsoft.Office.Interop.Word.WdProtectionType .wdAllowOnly Reading,ref noReset,ref password,ref useIRM,ref enforceStyleLock);

But I want to make the Paragraph or range to readonly

foreach (Microsoft.Office.Interop.Word.Paragraph aPar in 
                    _appObject.ActiveDocument.Paragraphs)
{
Microsoft.Office.Interop.Word.Range parRng = aPar.Range;
string sText = parRng.Text;
// I want to make readonly  here
}

然后文档将被保存 .

_DocObj.SaveAs(FileName: TargetDir, FileFormat: WdSaveFormat.wdFormatDocumentDefault);
            object saveChanges = WdSaveOptions.wdSaveChanges;
            object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
            object routeDocument = true;
            islockStatus = true;
 var doc_close = (Microsoft.Office.Interop.Word._Document)_DocObj;
 doc_close.Close(ref saveChanges, ref originalFormat, ref routeDocument);

因此,要求就像制作word文档的一部分(特别是HEADING或段落或变更范围)

1 回答

  • 0

    如果您有 Range 对象,则可以使用 Editors 成员访问允许编辑该范围的用户列表 .

    在您的情况下,您希望启用“everyone”来编辑整个文档,然后删除编辑特定段落的权限 .

    在VBA中,这看起来像这样(我相信你可以把它翻译成C#):

    ' Allow access to the entire doc
    ActiveDocument.Content.Editors.Add wdEditorEveryone
    
    ' Remove access to paragraph 1
    ActiveDocument.Content.Paragraphs(1).Editors(wdEditorEveryone).Delete
    

相关问题