Welcome to the WinForms feedback portal. We’re happy you’re here! If you have feedback on how to improve the WinForms, we’d love to hear it!

  • Check out the features or bugs others have reported and vote on your favorites. Feedback will be prioritized based on popularity.
  • If you have feedback that’s not listed yet, submit your own.

Thanks for joining our community and helping improve Syncfusion products!

1
Vote

Hi there,

I have a destination doc which contains bookmarks which should be replaced with another source doc. I clone the content of the source doc into a TextBodyPart and use it for a call to ReplaceBookmarkContent. That works so far.

But if the destination and source doc contains a style with the same name a new style is added with a GUID as a postfix by the ReplaceBookmarkContent method. My business case is that the style of the destination doc has priority and this style should applied. So that there arent't any adiitional styles with cryptic names.

So I tried to walk through all doc entities after the replacement and check if there is a style which ends with a GUID. In that case I check if there is a style with the same name without the GUID and if yes, I assign that style to the entity. After doing this I want to delete all the styles with that GUID. But if I call the Remove method of the style an InvalidOperationException is thrown:


// Delete uniquely named styles which are substituted by the styles of the destination document
foreach (string styleToDelete in stylesToDelete)
{
   IStyle style = wordDocument.Styles.FindByName(styleToDelete);
   if (style != null)
   {
      // If I save and close the document before calling CleanupStyles no exception is thrown here,
     // but the style still exists after calling style.Remove()
      style.Remove();
   }
}


Like you see in the comment the exception is not thrown when I save and close the document before doing the styles manipulation (which is not an option because of performance reasons) but then the call doesn't delete the style at all! The style collection is the the same like before the call of Remove() even if no exception occures.

Please see the sample I've attached.


Or is there a better solution to prevent that the styles with the GUIDs are added when a style in the destination and source document has the same name. Destination style should win and should be applied.

Kind regards,
Christian