Openxml SDK

fatima zaka 0 Reputation points
2023-08-29T10:36:30.4766667+00:00

Hey, I am trying to remove an imbedded ole object in my word document through open xml sdk, but my file is corrupted after code execution. I am using the following code to iterative remove ole objects in my word file:

WordprocessingDocument doc = WordprocessingDocument.Open(inputFilePath, true);
      {
       MainDocumentPart mainPart = doc.MainDocumentPart;
       // Iterate through the parts and remove embedded objects
       if (mainPart.Document.Body.Descendants<DocumentFormat.OpenXml.Vml.Office.OleObject>().Any())
        {
        foreach (var oleObj in mainPart.Document.Body.Descendants<DocumentFormat.OpenXml.Vml.Office.OleObject>())
         {
           oleObj.Remove(); 
         }
           //Delete the embedded objects. This will remove the actual attached files from the document.
           mainPart.DeleteParts(mainPart.EmbeddedObjectParts);
           //Delete all picture in the document
           mainPart.DeleteParts(mainPart.ImageParts);
        }
Community Center Not monitored
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.