Félix Chénier PhD


Export from Scrivener 2 to Word with structured heading styles

Publié le 2014-11-08

Scrivener is a great non-linear writing tool to organize and develop ideas while writing. I use it exclusively to write all my scientific publications. Whereas I usually use it to generate LaTeX code (I will write an article on it soon), sometimes it is more appropriate to export from Scrivener to Word. Scrivener does provide this functionality, but section titles in the exported document are not styled with Word heading styles. Therefore, it is very time consuming to manually go around every titles in the exported document to convert them in Title 1, Title 2, Title 3, etc. styles.

Since I find it quite frustrating, I looked around and found this website explaining this same problem, with something very close to a solution for me, but that did not work as expected on Word 2011 for Mac. Based on this solution, I built my own and posted it here so it can help others.


1. Writing and structuring in Scrivener

Here is a sample project in Scrivener. Have a look at the hierarchical titles in the binder. Have also a look at the bolds, italics and underlines in the text. We want to preserve both in the Word document.



2. Exporting to Word

Exporting to word using the ScrivenerToWord preset included later in this post, this project is outputted in this Word document. Headings are in large and bold characters, but are not still styled with the corresponding Word styles (Title, Title 1, Title 2, etc.).



3. Restyling

Running the ScrivenerToWord Word macro also included later in this post, each heading is converted to its appropriate style based on the #, ##, ###, etc. that were added at the end of the headings. It may take a little time for a long document, but it is okay for me because it is automatic. Body font is also changed to match the current style, and bolds, italics and underlines are kept.



Configuring Scrivener and Word


Download this compile setting, unzip it and import it in your custom compile settings. In Scrivener 2.6, this is done following these steps:


Create a macro with this code in it:

Sub FormatScrivener()
    FormatWithStyle sTextToFind:="~#~", oStyleToUse:=ActiveDocument.Styles(wdStyleTitle)
    FormatWithStyle sTextToFind:="~##~", oStyleToUse:=ActiveDocument.Styles(wdStyleHeading1)
    FormatWithStyle sTextToFind:="~###~", oStyleToUse:=ActiveDocument.Styles(wdStyleHeading2)
    FormatWithStyle sTextToFind:="~####~", oStyleToUse:=ActiveDocument.Styles(wdStyleHeading3)
    FormatWithStyle sTextToFind:="~#####~", oStyleToUse:=ActiveDocument.Styles(wdStyleHeading4)
    FormatWithStyle sTextToFind:="~######~", oStyleToUse:=ActiveDocument.Styles(wdStyleHeading5)
End Sub

Sub FormatWithStyle(sTextToFind As String, oStyleToUse As Style)
    Dim oRange As Range
    Set oRange = ActiveDocument.Range
    With oRange
        With .Find
            .Text = sTextToFind
            .Format = True
            .Replacement.Style = oStyleToUse
            .Replacement.Text = " "
            .Forward = True
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
    End With
End Sub

Sub ResetFormatting()
    Dim p As Paragraph
    For Each p In ActiveDocument.Paragraphs
        If p.Range.Style = ActiveDocument.Styles(wdStyleHeading1) Then
            p.Range.Style = ActiveDocument.Styles(wdStyleHeading1)
        ElseIf p.Range.Style = ActiveDocument.Styles(wdStyleHeading2) Then
            p.Range.Style = ActiveDocument.Styles(wdStyleHeading2)
        ElseIf p.Range.Style = ActiveDocument.Styles(wdStyleHeading3) Then
            p.Range.Style = ActiveDocument.Styles(wdStyleHeading3)
        ElseIf p.Range.Style = ActiveDocument.Styles(wdStyleHeading4) Then
            p.Range.Style = ActiveDocument.Styles(wdStyleHeading4)
        ElseIf p.Range.Style = ActiveDocument.Styles(wdStyleHeading5) Then
            p.Range.Style = ActiveDocument.Styles(wdStyleHeading5)
        ElseIf p.Range.Style = ActiveDocument.Styles(wdStyleTitle) Then
            p.Range.Style = ActiveDocument.Styles(wdStyleTitle)
            p.Range.Font.Name = p.Range.Style.Font.Name
            p.Range.Font.Size = p.Range.Style.Font.Size
        End If
End Sub

In Microsoft Word 2011 for Mac, this is done following these steps:

From now on, you should be able to convert a Scrivener document into a Word document while preserving heading styles and bold, italics and underlines in text. Feel free to contact me for questions or comments.