2012-05-23 60 views

回答

5

的OPENXML SDK提供了Lock類和LockingValues枚舉 的選項編程設定:

  • 內容控制不能被刪除,
  • 內容不能被編輯

所以,將這兩個選項設置爲「假」(LockingValues.Unlocked), 搜索文檔中的所有SdtElement元素並設置Val property to LockingValues.Unlocked

下面的代碼展示了一個例子:

static void UnlockAllSdtContentElements() 
{ 
    using (WordprocessingDocument wordDoc = 
    WordprocessingDocument.Open(@"c:\temp\myword.docx", true)) 
    {   
    IEnumerable<SdtElement> elements = 
     wordDoc.MainDocumentPart.Document.Descendants<SdtElement>(); 

    foreach (SdtElement elem in elements) 
    { 
     if (elem.SdtProperties != null) 
     { 
     Lock l = elem.SdtProperties.ChildElements.First<Lock>(); 

     if (l == null) 
     {    
      continue; 
     } 

     if (l.Val == LockingValues.SdtContentLocked) 
     { 
      Console.Out.WriteLine("Unlock content element..."); 
      l.Val = LockingValues.Unlocked; 
     } 
     } 
    } 
    } 
} 

static void Main(string[] args) 
{ 
    UnlockAllSdtContentElements(); 
} 
1

就爲了誰複製該代碼,請記住,如果沒有相關的內容控制鎖,那麼就不會有一個鎖定的那些屬性關聯到它,因此,當該代碼執行以下指令,它將返回一個例外,因爲,沒有找到元素:)

鎖升= elem.SdtProperties.ChildElements.First(;

解決此問題的方法是執行FirstOrDefault而不是First。

+0

應該添加爲相關解決方案的評論 –

+0

嗨@OriPrice,我不能評論解決方案,因爲我的聲望低於50。 –

相關問題