Skip to content

Instantly share code, notes, and snippets.

@jjxtra
Last active July 11, 2024 14:56
Show Gist options
  • Save jjxtra/5065efe8a0c0def823512afab77fd2e3 to your computer and use it in GitHub Desktop.
Save jjxtra/5065efe8a0c0def823512afab77fd2e3 to your computer and use it in GitHub Desktop.
Fix Scrivener Longlasting Stupidity
' - Scrivener has a host of bugs and is rarely updated--this document will be my knowledge share with my writing on how I've worked around this.
' - Always compile to rtf.
' - Load rtf in word.
' - Save as docx.
' - Open print dialog and close it to force table of contents to update.
' - Highlight all text, open layout -> paragraph -> check keep lines together.
' - Do same as above, but in a footer.
' - On the File tab, go to Options > Customize Ribbon. Under Customize the Ribbon and under Main Tabs, select the Developer check box.
' - Press Alt + F11 to open the VBA editor in Word.
' - In the VBA editor, go to Insert > Module to create a new module.
' - Paste in the following text, save, and then back in word, go to developer tab, click macros, and run any as needed.
Sub ConvertPlainTextURLsToHyperlinksInFooters()
Dim sec As Section
Dim ftr As HeaderFooter
Dim ftrRange As Range
Dim regex As Object
Dim matches As Object
Dim match As Object
Dim startPos As Long
Dim endPos As Long
Dim url As String
Dim rng As Range
' Define a simple URL pattern
Set regex = CreateObject("VBScript.RegExp")
With regex
.Global = True
.IgnoreCase = True
.Pattern = "(http|https)://[^\s]+"
End With
' Loop through each section in the document
For Each sec In ActiveDocument.Sections
' Loop through each footer in the section
For Each ftr In sec.Footers
' If the footer is not empty
If ftr.Range.Text <> vbCr Then
Set ftrRange = ftr.Range
' Find matches
If regex.test(ftrRange.Text) Then
Set matches = regex.Execute(ftrRange.Text)
' Loop through matches in reverse to avoid messing up positions
For i = matches.Count - 1 To 0 Step -1
Set match = matches(i)
startPos = match.FirstIndex + 1
endPos = startPos + match.Length - 1
url = match.Value
' Set the range for the URL
Set rng = ftrRange.Characters(startPos)
rng.End = ftrRange.Characters(endPos).End
' Add hyperlink
ftrRange.Hyperlinks.Add Anchor:=rng, Address:=url, TextToDisplay:=url
Next i
End If
End If
Next ftr
Next sec
MsgBox "All plain text URLs in footers have been converted to hyperlinks."
End Sub
Sub RemoveFirstBlankLineEachPage()
'Scrivener compile forces a blank line on pages. This undoes it.
Dim doc As Document
Set doc = ActiveDocument
Dim i As Integer
Dim rngParagraph As Range
Application.ScreenUpdating = False
For i = 1 To doc.ComputeStatistics(wdStatisticPages)
' Go to the beginning of each page
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i
Selection.HomeKey Unit:=wdLine
' Set range to the first paragraph in the page
Set rngParagraph = Selection.Paragraphs(1).Range
' Check if the paragraph is empty
If Len(Trim(rngParagraph.Text)) = 1 Then
If Asc(rngParagraph.Text) = 13 Then ' Check if it is a carriage return
If Selection.Paragraphs.Count = 1 Then
rngParagraph.Delete
ElseIf rngParagraph.ParagraphFormat.Alignment = wdAlignParagraphJustify And Selection.Paragraphs(2).Range.ParagraphFormat.Alignment <> wdAlignParagraphCenter Then
rngParagraph.Delete
End If
End If
End If
Next i
Application.ScreenUpdating = True
MsgBox "Blank lines at start of pages have been removed."
End Sub
Sub ChangeFootnoteNumbersTo10Point()
Dim footnote As footnote
Dim storyRange As Range
Dim footnoteRange As Range
' Change the font size of the footnote reference numbers in the main text
For Each footnote In ActiveDocument.Footnotes
footnote.Reference.Font.Size = 10
Next footnote
' Change the font size of the footnote numbers at the bottom of the page
For Each storyRange In ActiveDocument.StoryRanges
If storyRange.StoryType = wdFootnotesStory Then
Set footnoteRange = storyRange
footnoteRange.Font.Size = 10
End If
Next storyRange
' Inform the user that the operation is complete
MsgBox "All footnote numbers have been changed to 10 point."
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment