C#限制编辑部分单词段落
本文关键字:单词段 编辑部 | 更新日期: 2023-09-27 18:05:54
允许用户编辑黄色段落。但我收到了这份文件:错误文件
我使用单词限制编辑功能,我的代码是:
//写入段落
foreach (TaskChaptersModel chapter in listChapter)
{
paragraph = doc.Content.Paragraphs.Add();
paragraph.Range.Text = chapter.chapterNo + " " + chapter.chapterName;
//标题1
if (!chapter.chapterNo.Contains("."))
{
paragraph.Range.Font.Name = "宋体";//宋体
paragraph.Range.Font.Bold = 2;//加粗
paragraph.Range.Font.Size = 16;//三号
paragraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
}
else if (chapter.chapterNo.Select(c => c == '.').Count() == 1)
{
paragraph.Range.Font.Name = "宋体";//宋体
paragraph.Range.Font.Bold = 2;//加粗
paragraph.Range.Font.Size = 16;//三号
paragraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//左对齐
paragraph.Format.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
}
else
{
paragraph.Range.Font.Name = "宋体";//宋体
paragraph.Range.Font.Size = 12;//三号
paragraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//左对齐
paragraph.Format.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
}
paragraph.Range.InsertParagraphAfter();
//-------------------------------------------------------------------
//This paragraph alow edit
paragraph = doc.Content.Paragraphs.Add();
paragraph.Range.Text = "请您编辑,张三";
paragraph.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone);
paragraph.Range.InsertParagraphAfter();
//-------------------------------------------------------------------
}
doc.Protect(Word.WdProtectionType.wdAllowOnlyReading, false, "000", false, true);
我的想法是在指定段落添加编辑器。
您的想法一开始是正确的,但是Word的行为与预期有点不同。我注意到,就像你一样,无论添加新段落,新段落都可以编辑,即使你没有特别在新段落中添加编辑。
因此,你需要扭转局面,将每个人都添加到完整的文档中,并从你想要保护的部分中删除编辑器。
为了给你一个想法,我创建了一小段VBA代码,但我相信你可以用C#重写你需要的部分。还要注意,我循环遍历编辑器,逐个删除它们,而不是使用DeleteAll((。我发现使用DeleteAll会引发意外的异常。
Sub TestRestrictDocument()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect "000"
End If
ActiveDocument.Content.Editors.Add Word.WdEditorType.wdEditorEveryone
Dim paragraph As paragraph
For I = 1 To 5
Set paragraph = ActiveDocument.Paragraphs(I)
If I <> 4 Then
paragraph.Range.Select
Dim objEditor As Editor
If Selection.Editors.Count > 0 Then
For X = 1 To Selection.Editors.Count
Selection.Editors(X).Delete
Next
End If
End If
Next
ActiveDocument.Protect Word.WdProtectionType.wdAllowOnlyReading, False, "000", False, True
End Sub
这个例子向您展示了(我创建了一个有5段要测试的文档(除了第四段之外,其他所有段落都可以编辑。显然,您可以动态地创建文档并删除文档上的编辑器。
我遇到的一些旁注可能会有所帮助:
- 始终检查您的文档是否已受到保护
- 始终测试范围是否有要删除的编辑器
- 出于某种原因避免在编辑器上删除全部
明白了:1、 创建文档2、 循环所有段落在右侧段落添加编辑器我发现在创建文档时无法添加编辑器。
//写入段落
foreach (TaskChaptersModel chapter in listChapter)
{
if (chapter.chapterNo == null || chapter.chapterNo == "-1")
{
continue;
}
paragraph = doc.Content.Paragraphs.Add();
paragraph.Range.Text = chapter.chapterNo + " " + chapter.chapterName;
//标题1
if (!chapter.chapterNo.Contains("."))
{
paragraph.Range.Font.Name = "宋体";//宋体
paragraph.Range.Font.Bold = 2;//加粗
paragraph.Range.Font.Size = 16;//三号
paragraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
}
else if (chapter.chapterNo.Select(c => c == '.').Count() == 1)
{
paragraph.Range.Font.Name = "宋体";//宋体
paragraph.Range.Font.Bold = 2;//加粗
paragraph.Range.Font.Size = 16;//三号
paragraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//左对齐
paragraph.Format.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
}
else
{
paragraph.Range.Font.Name = "宋体";//宋体
paragraph.Range.Font.Size = 12;//三号
paragraph.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//左对齐
paragraph.Format.LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5;
}
paragraph.Range.InsertParagraphAfter();
paragraph = doc.Content.Paragraphs.Add();
//有编辑权限
if (chapter.wordFlag == "E")
{
paragraph.Range.Text = "请您编辑," + userName;
}
else
{
paragraph.Range.HighlightColorIndex = Word.WdColorIndex.wdGray25;
}
paragraph.Range.InsertParagraphAfter();
}
foreach (Word.Paragraph p in doc.Paragraphs)
{
if (p.Range.Text.Contains("请您编辑"))
{
p.Range.Select();
p.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone);
}
}