Saturday, May 28, 2011

Revert to Saved File

Choosing Edit, Undo (or better yet, using Ctrl-Z) is all very well for a few changes, but a professional saves their work every time a significant change has been made, or every time they embark on a risky manoeuvre.
Some of us save our work before we pick up the phone, or as we hear a colleague approaching.
That means that our saved file is the best source of recovering from a failed experiment.

Public Sub FileRevertToSaved()
If Len(ActiveDocument.Path) = 0 Then
MsgBox "You can not revert to an unsaved file."
Else
If MsgBox("Are you sure that you want to abandon your current work?", vbYesNo) = vbNo Then
Else
Documents.Open FileName:=ActiveDocument.FullName, revert:=True
End If
End If
End Sub

Signature at foot of page

The documents bear a signature on page 2 of a 4-page document, and we wanted to maximize the amount of white space before the signature, by pushing the signature to the foot of the signature page.
Place the cursor in the signature (2nd-last paragraph of page 2) and run the macro SpaceBefore.

Sub SpaceBefore()
' Increase paragraph-spacing-before the first Paragraph of the selection to fill up the current page of the document
Dim prg As Paragraph
Set prg = Selection.Paragraphs(1)
Dim lngSpaceAbove As Long
lngSpaceAbove = prg.SpaceBefore
Dim lngPages As Long
lngPages = prg.Parent.Range.Information(wdActiveEndPageNumber)
While lngPages = prg.Parent.Range.Information(wdActiveEndPageNumber)
lngSpaceAbove = lngSpaceAbove + 1
prg.SpaceBefore = lngSpaceAbove
Wend
prg.SpaceBefore = lngSpaceAbove - 1
End Sub

Minutes later I decided to add some text to the document, pushing the signature paragraph over to page 3, so ...

Place the cursor in the signature (2nd-last paragraph of page 2) and run the macro DecreaseSpaceBefore.

Sub DecreaseSpaceBefore()
' Decrease paragraph-spacing-before the first Paragraph of the selection to pull the current page up by one page.
' Use this after inserting extra text after running "IncreaseSpaceBefore"
Dim prg As Paragraph
Set prg = Selection.Paragraphs(1)
Dim lngSpaceAbove As Long
lngSpaceAbove = prg.SpaceBefore
Dim lngPages As Long
lngPages = prg.Parent.Range.Information(wdActiveEndPageNumber)
While lngPages = prg.Parent.Range.Information(wdActiveEndPageNumber)
lngSpaceAbove = lngSpaceAbove - 1
prg.SpaceBefore = lngSpaceAbove
Wend
End Sub

Thursday, May 26, 2011

Keep with before

Don't get me started on Microsoft ...
I have a simple document with "Heading 1", "Heading 2" and "Heading 3" styled paragraphs, and within each heading, text in "Body Text" style.
Sadly some of the Body Text material splits across a page, and I'd rather have all the Body text on the same page as its immediate parent "Heading".
Office 2003 hasn't had "Keep With Before" for at least the last 8 years, so I wrote the attached.
Use:
Place the text cursor anywhere in any paragraph of YOUR text style; it doesn't have to be Body Text.
Run the macro TESTKeepWithBeforeSelection.
Method:
I'm using Selection so you can enjoy the blinking lights.
I leave your original selection in place when I'm done.
I use the style of the FIRST paragraph in your selection as my guiding light.
I turn OFF KeepWithNext and turn ON KeepTogether in every paragraph in the document.
Then for each paragraph in your chosen style, I backup one paragraph to the preceding paragraph and turn ON KeepWithNext.
This seems to work for me.
Function KeepWithBeforeSelection(strStyle As String)
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(strStyle)
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Selection.Find.Execute
Selection.MoveUp Unit:=wdParagraph, Count:=2
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
With Selection.ParagraphFormat
.KeepWithNext = True
End With
Selection.MoveDown Unit:=wdParagraph, Count:=2
Wend
End Function
Sub TESTKeepWithBeforeSelection()
Dim rng As Range
Set rng = Selection.Range
Dim strStyle As String
strStyle = Selection.Paragraphs(1).Range.Style.NameLocal
Selection.WholeStory
With Selection.ParagraphFormat
.KeepWithNext = False
.KeepTogether = True
End With
Selection.HomeKey Unit:=wdStory
Call KeepWithBeforeSelection(strStyle)
rng.Select
End Sub