January, 2010

  • Eric White's Blog

    Programmatically Limiting Styles in Word


    Authoring in Word makes publishing systems very user friendly.  After the user authors his or her paper, you can transform Open XML WordprocessingML to the desired output format.  Some publishing systems use an approach of limiting the paragraph and character styles that the author can use.  This can help in writing a more deterministic transform to the output format.  Alternatively, you can provide a resilient transform that implements some reasonable approach for handling user-defined styles, but this may lead to confusion on the part of the author if the behavior was not what was expected.  But if you decide to limit styles, there are (at least) three approachs:

    This blog is inactive.
    New blog: EricWhite.com/blog

    Blog TOC
    The following VBA macro limits styles to a set of four possible styles, and locks the document so that authors can use only those four styles in a document:

    Dim cnt As Integer
    For Each Style In ActiveDocument.Styles
        Style.Locked = True

    ActiveDocument.Styles("Heading 1").Locked = False
    ActiveDocument.Styles("Heading 2").Locked = False
    ActiveDocument.Styles("Normal (Web)").Locked = False
    ActiveDocument.Styles("Title").Locked = False
    ActiveDocument.Protect Password:="password", NoReset:=False, Type:= _
        wdNoProtection, UseIRM:=False, EnforceStyleLock:=True

    You can write managed add-in code to do the same thing:

    Word.Document currentDocument = doc;
    foreach (Word.Style style in doc.Styles)
        if (style.NameLocal == "Heading 1" ||
            style.NameLocal == "Heading 2" ||
            style.NameLocal == "Title" ||
            style.NameLocal == "Normal")
            style.Locked = false;
            style.Locked = true;
    object noReset = (object)false;
    object password = (object)"password";
    object useIrm = (object)false;
    object enforceStyleLock = (object)true;
        ref noReset, ref password, ref useIrm, ref enforceStyleLock);

    Another approach is to directly modify documents using the Open XML SDK to limit styles.

    To modify a document, limiting its styles, you need to set the w:documentProtection element in the Settings part.  In the Styles part, you update the w:defLockedState attribute of the w:latentStyles element, and the w:locked attribute of appropriate w:lsdException elements.

Page 2 of 10 (10 items) 12345»
Page 1 of 1 (10 items)