Make It Work #1: Making Scrivener Files Work with Word Table of Contents (#scrivener #selfpublishing)

Greetings Friends and fellow Scribes!

I’ve got a how-to video for you!  If you’ve been frustrated by the table of contents not working right when going fro Scrivener to Word then this little demonstration is for you!

The other day when I was editing, compiling, making a mistake, and repeating about a dozen times, I found myself confounded by another problem.

Word wouldn’t make a table of contents.
WHY?

Watch the video below and then you can come back here and copy the Macro code at the bottom of this  post. Easy as pi (nope, didn’t spell that wrong).

So what brought here? I like to keep it simple. Double space, 12 point font, bold, italic, underline, and headings. I haven’t done fancy shenanigans like document merging in word for at least eight years. I can assure you that, at the time, I was still using Word 2002. I hardly ever venture into the menu entrails.

Okay, Google:
Why won’t #&*(ing Word create a table of contents?

Google:
Word uses headings to populate the table of contents.

Okay, Google:
How do you create a TOC from Scrivener to Word?

Google:

Okay, Google:
Google? You there?

Google:
You can’t with the Windows version of World.

If I had been at my desk, I would have bludgeoned myself. I spent an hour or two researching Scrivener skills and compiling to Word and PDF.  All the work-arounds were painful and required Microsoft Office for the easy way or OneDrive and a million steps for for the convoluted way.  I did use Calibre to go from an epub file to PDF, and the table of contents worked fine, but I still had a problem with the word document. I wanted it to work because I wanted to be able to manipulate the PDF pages better.

I happened to stumble upon a video. The dude used a macro. Except, I had to type it out by hand from the screen.  It was fuzzy.  Is that an e or and s?   Thankfully, my faded background in BASIC, HTML, and a short foray into manipulating Javascript helped.  I was able to get it working.

Hopefully, all you have to do is cut-and-paste and let the magic happen.


MACRO MAGIC

TOC MACRO BELOW (Do not copy the title Sub TOCHeadingChange() or the last End Sub — those are automatically generated when you CREATE your macro.  Please note that in the string “#@# ” . You can change this nonsense string to whatever you want. Just be sure you use the same symbols in the RemovePrefix Macro. You can change “Heading 1” to another heading (1-4 or 1-6 depending on your Word).

Sub TOCHeadingChange()
'
' TOCHeadingChange Macro
' SOURCE: https://www.youtube.com/watch?time_continue=675&v=J5r9zXc6tyg
'
Dim sTextToFind As String
Dim oSelection As Style
Dim oRange As Range

sTextToFind = "#@#"
Set oStyleToUse = ActiveDocument.Styles("Heading 1")
Set oRange = ActiveDocument.Range

With oRange

    'clear previus format criteria in the find dialog
    .Find.ClearFormatting
    .Find.Replacement.ClearFormatting

    With .Find
         .Text = sTextToFind
         .Forward = True
         .Wrap = wdFindStop
         .Format = False

         Do While .Execute()
            'Change the Style
            oRange.Paragraphs(1).Style = oStyleToUse
        Loop
    End With
End With

End Sub

PREFIX REMOVING MACRO BELOW (Do not copy the title Sub DeletePrefix() or the last End Sub — those are automatically generated when you CREATE your macro. Please note that in the string “#@# “ has a space before the second quote. You can change this nonsense string to whatever you want but keep the space so that it can be removed properly by the Macro.

Sub DeletePrefix()
'
' DeletePrefix Macro
' SOURCE: https://stackoverflow.com/questions/30871924/deleting-text-from-word-macro
'

    Dim sample
    sample = "#@# "

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = sample
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

 


WANT THE “REMOVE DOUBLE SPACES MACRO”?

I got it direct, with no alternation from the Spreadsheet Guru’s Vault written by Ryan Wells (wellsr.com)

The direct link to the code is here: https://www.thespreadsheetguru.com/the-code-vault/vba-code-to-remove-double-spaces-in-word


HOW DID YOU MAKE THE “DELETESUFFIX” and “TOCSubheading” MACROs?

I created a new macro and named it DeleteSuffix and another TOCsubheadingChange and copied and pasted the original code from DeletePrefix and TOCheadingChange macro.

I then changed the find string ”  ” in DeletePrefix to something else such as ” #@@#” because I wanted my subheading to be different and I changed TOCsubheading to “Heading 2″.  Use whatever you want.

You don’t have to create a new macro, you can edit the existing one by changing the find string ”  ” to whatever you want before you run it.  I’m lazy so…  I made duplicates and then recorded a super macro called “ParentMacro” (I just hit the record macro button) and ran them one at a time. Saved it.

Poof. One click magic.

What’s next?
CustomizingYour Book's Table of Contents in Word


Inquiring minds want to know:

Did I make your day?

Don’t forget to share with your friends who want Scrivener and Word to play nice in Windows.

Advertisements

3 thoughts on “Make It Work #1: Making Scrivener Files Work with Word Table of Contents (#scrivener #selfpublishing)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s