We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

NullReferenceException when saving document with imported sections


When saving a document created by cleaning styles and importing sections from another document we get NullReferenceException. For reproducing the issue please use the attached docx files and C# file containing demo code (changed extension to txt to upload it here).

Best regards,
Aurelian Iordache
IBM Romania

Hi Aurelian,

Thanks for your inquiry. The following highlighted code snippet causes the exception of this issue. Could you please share some detail about your query what exact you want to achieve in DeleteStyle method? We will then provide you more information about your query along with code.

public static void DeleteStyle(Style style)
    Style replaceWith;
    switch (style.Type)
        case StyleType.Paragraph:
            replaceWith = style.Styles[StyleIdentifier.Normal];
        case StyleType.Character:
            replaceWith = style.Styles[StyleIdentifier.DefaultParagraphFont];
        case StyleType.Table:
            replaceWith = style.Styles[StyleIdentifier.TableNormal];
        case StyleType.List:
            replaceWith = style.Styles[StyleIdentifier.NoList];
            throw new ArgumentOutOfRangeException();
    var normalName = replaceWith.Name;
    replaceWith.Name = style.Name; // changing the style name replaces the style with the same name 
    replaceWith.Name = normalName;

Hi Tahir,

Basically what we want to achieve is: starting with an existing document we want to keep all the properties and formatting of the document but to delete its content and also to delete not used styles except the minimum required styles (see CreateEmptyDocument method).
What we’re actually doing in DeleteStyle method is to replace all the extra-styles with one of the 4 styles from the minimum required set of styles.
After this we gonna use this empty document as a base document where we import sections from other documents (at import operations those sections will add to this base document their own styles necessary do be displayed correctly).
This worked so far, this is the first document where we get the NullReferenceException.
As you located in the Aspose code the location and the cause of this exception what would be your recommended changes in our code to avoid the exception for this document and possibly another documents like this in the future ?

Thank you,
Aurelian Iordache
IBM Romania

Hi Aurelian,

Thanks for sharing the detail. I have logged this problem in
our issue tracking system as WORDSNET-10866. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-10866) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.