locked
Deleting Content Controls from the Word Document in docx file RRS feed

  • Question

  • Hi everyone. 
    I'm trying to get read of the Content Controls in input docx files. Have a method to find and Delete them (below).  With it I was able to find them but when I do  SdtRun.Remove() nothing happens, and they are still there.  Not sure if where is and what I do incorrectly. 

    public void DeleteContentControls()
    {
      MainDocumentPart main = wordDocument.MainDocumentPart;
      Console.WriteLine("Looking for Content controls to be deleted");
      IEnumerable<SdtRun> sdtElement = main.Document.Body.Descendants<SdtRun>()
      Console.WriteLine(sdtElement.Count());
    
      foreach (SdtRun sdtEl in sdtElement)
       {
         Console.WriteLine(sdtEl);
         sdtEl.Remove();
       }
    
    }
    
    public void SaveAndClose()
    {
      wordDocument.MainDocumentPart.Document.Save();
      wordDocument.Close();
    }

    Another thing is that it doesn't save any changes , through SaveAndClose method, and stops with empty error message. Probably due to the fact that no changes happening. 
    Monday, December 14, 2020 3:23 AM