Cannot find a style with this style identifier - StyleIdentifier.SubtleEmphasis

Hi,

I’m getting an exception saying “Cannot find a style with this style identifier.” when attempting to set this style in a document I’m creating:

builder.CurrentParagraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.SubtleEmphasis;

Can you please comment on this.

Also, how can I check if a style exists or not?

Thanks,
Irfan

Hi

Thanks for your request. This occurs because you document does not contain such style. You can use code like the following to check whether style exists:

// Get style.
Style style = doc.Styles[StyleIdentifier.SubtleEmphasis];
// Check whether style exists
if (style != null)
{
    // Do something
}

Hope this helps.
Best regards.

Thanks Alexey,

I am curious as to why that style identifier is enumerated if the style does not exist in the document? If I am creating a new document, is there a way to load this style from somewhere, or is my only choice to create this style.

Regards,
Irfan

Hi

Thanks for your request. There are a lot of styles listed in StyleIdentified enum:
https://reference.aspose.com/words/net/aspose.words/styleidentifier/
They all are not required to be embedded in document. MS Word creates them when it is necessary. The main purpose of StyleIdentifier is language independence (see documentation).
Best regards.