Here is a method to create a Word Macro that deletes the paragraph mark above a specific style in Word. I create a style in Scrivener called Chapter Title and assign it in the compile settings to the section title before compiling. Then I add the VBA macro below to my Word document and run it and voila! All the blank lines above my chapter titles are gone.
Below is a VBA macro that you can use in Word to find every instance of a specific style and delete the paragraph (line) just before it. Please replace "YourStyleName"
with the actual name of the style you’re targeting:
vbCopy code
Sub DeleteParagraphBeforeStyledText()
Dim oPara As Paragraph
Dim sStyle As String
' Set the style name you're looking for
sStyle = "YourStyleName"
' Loop through all paragraphs in the document
For Each oPara In ActiveDocument.Paragraphs
' Check if the paragraph style matches the specified style
If oPara.Style = sStyle Then
' Check if there is a previous paragraph
If Not oPara.Previous Is Nothing Then
' Delete the entire previous paragraph
oPara.Previous.Range.Delete
End If
End If
Next oPara
End Sub
Here’s how you can add and run this macro in Word:
- Press
Alt + F11
to open the VBA editor. - In the Project window, find your document’s name and right-click on it. Choose
Insert
>Module
to create a new module. - In the new module window, paste the code provided above.
- Replace
"YourStyleName"
with the name of the style you want to target. Make sure the name matches exactly, including capitalization. - Close the VBA editor.
- You can now run the macro by pressing
Alt + F8
, selectingDeleteParagraphBeforeStyledText
from the list, and clickingRun
.
Note that this will delete the paragraph before every instance of the specified style throughout the entire document.