Processing
 

C# restrict editing partial word paragraph

07/09/2016 07:38#1

mymbrooks

Member

Joined at: 4 months ago

Post: 2

Thank: 0

Thanked: 0

allow users to edit the yellow paragraph. But I get this document: wrong document

I use word restrict editing feature, my code is :

//写入段落 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); 

My idea is to add editor at Specified paragraph.

07/09/2016 14:47Top#2

Joined at: 10 months ago

Post: 15

Thank: 0

Thanked: 0

Your idea is right to start with, however Word acts a little bit different than expected. I noticed, just like you, that however adding new paragraphs the new paragraps could be edited even though you didn't specifically added Editors to the new paragraph.

So, you need to turn it around, by adding everyone to the complete document and working the other way around by removing the editors from the parts that you want to protect.

To give you an idea I created a little piece of VBA code but I'm sure you can rewrite the parts you need in C#. Also note that I loop over the editors deleting them individually instead of using DeleteAll(). I found that using DeleteAll was throwing unexpected exceptions.

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 

This example shows you that (I created a document with 5 paragraphs to test) all but the fourth paragraphs can be edited. Obviously you can dynamically create your document and remove the Editors on that.

Some side notes to keep in mind that I ran into that might help:

  • always check if your document is already protected
  • always test if the range has Editors to delete
  • Avoid DeleteAll on Editor for some reason
20/09/2016 12:11Top#3

mymbrooks

Member

Joined at: 4 months ago

Post: 2

Thank: 0

Thanked: 0

Got it: 1、create the doc 2、loop all paragraphs add editor at the right paragraph I found that I can't add editor when the doc is create.

//写入段落

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);     } } 
Similar articles

VBA regex matching over two lines

4 months ago - Reply: 1 - Views: 44

Table output to Word-2011 from Rstudio using knit

4 months ago - Reply: 0 - Views: 32

Write a Collection of paragraphs into a new document

4 months ago - Reply: 1 - Views: 70

how to write paragraphs without show to document

4 months ago - Reply: 1 - Views: 66

numericals in word macro

4 months ago - Reply: 1 - Views: 45

Can not find proofing tools

4 months ago - Reply: 0 - Views: 1

Possible values for <w:outlineLvl> in Word OOXML

4 months ago - Reply: 1 - Views: 7

C# restrict editing partial word paragraph

5 months ago - Reply: 2 - Views: 142

Macro translation Please (possible virus)

4 months ago - Reply: 1 - Views: 34

Doc/Docx Fastest Reading Method for Searching

4 months ago - Reply: 0 - Views: 1

Save embedded file / content from word

4 months ago - Reply: 0 - Views: 5

Counting words in Word document, including footnores

4 months ago - Reply: 1 - Views: 22

c# access extract word ole object

4 months ago - Reply: 1 - Views: 19

Using word wildcards to find unaccepted changes

4 months ago - Reply: 1 - Views: 45

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re