Method CreateStyleFromSelection
CreateStyleFromSelection(WordDocument, DocumentSpan, string, bool)
Create a Style from the selection: a new paragraph style named name, based on the first
paragraph's style, holding that paragraph's direct paragraph formatting and the direct run formatting at the
span's start, with a linked character style (name Char) holding the same run formatting, as Word adds
them. Every paragraph the span touches takes the new style, which is then reapplied to it.
public static DocumentEdit CreateStyleFromSelection(this WordDocument document, DocumentSpan span, string name, bool quickFormat = true)
Parameters
documentWordDocumentThe document.
spanDocumentSpanThe selection, or a caret.
namestringThe style's name, as the gallery shows it. Its id is the name without spaces and other characters an id does not take, with a number added when the id is taken.
quickFormatboolWhether the style is shown in the Styles gallery (
w:qFormat).
Returns
- DocumentEdit
The edit; undoing it removes both styles.
Exceptions
- ArgumentNullException
documentornameis null.- ArgumentException
The span is not in the document, the name is empty or longer than 253 characters, or a style already has the name (as Word compares names: ignoring case).