Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Deleting Content Controls from the Word Document in docx file

$
0
0
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. 

Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>