Saturday, June 25, 2011

Close All But Active

From time to time I find that I have opened a slew of documents, just to help me understand the one document I'm working on.
In this case I want to violate my Golden Rule of "Never Close, never Minimize", and close all open documents EXCEPT THE ACTIVE DOCUMENT without saving a single one; discard all recent changes.
This little macro does the trick.
Public Sub CloseAllButActive()
''' Close all open documents EXCEPT THE ACTIVE DOCUMENT without saving a single one; discard all recent changes.
If Documents.Count > 0 Then
Dim docSave As Document
Set docSave = ActiveDocument
Dim doc As Document
For Each doc In Application.Documents
If doc <> docSave Then
doc.Close savechanges:=wdDoNotSaveChanges
Else
End If
Next doc
Else ' nothing to do
End If
End Sub

Sunday, June 12, 2011

Jump To Document Folder

You've just saved your file, or you are wondering about another file in the same folder, so you need to open Windows Explorer and work your way through the trees to your folder.
That would be Alt-Tab to Explorer (or Start, Run, explorer.exe), click, double-click, click etc.
Right?
Wrong!
Use the little macro below

Public Sub JumpToDocumentFolder()
''' Launch Windows Explorer into the current document's folder.
Call Shell("explorer.exe /e," & ActiveDocument.Path, vbMaximizedFocus)
End Sub

Tuesday, June 7, 2011

Delete All Paragraphs This Style

From time to time I have occasion to remove globs of text from a document leaving bits behind.
For example, I might want to delete all 'Normal" styled paragraphs and leave only the 'Heading " styled paragraphs behind.
This macro does the trick, regardless of the styles in use!

Sub DeleteAllParagraphsThisStyle()
''' Place the text cursor in a paragraph, run the macro.
''' All paragraphs in that (selected) style will be removed from the document.
Dim strStyleName As String
strStyleName = Selection.Paragraphs(1).Range.Style
Dim lng As Long
For lng = ActiveDocument.Paragraphs.Count To 1 Step -1
If ActiveDocument.Paragraphs(lng).Style = strStyleName Then
ActiveDocument.Paragraphs(lng).Range.Delete
Else
End If
Next lng
End Sub

CloseAllTakeNoPrisoners

Although as a rule I "Never Close, Never Minimize", I confess to times when I have literally dozens of documents left open after a failed attempt to batch-process a folder.
At that time I like to fix the problem and start with a clean slate.
The little macro below shrugs and wipes my slate clean!

Public Sub CloseAllTakeNoPrisoners()
''' Close all open documents without saving a single one; discard all recent changes.
Dim doc As Document
For Each doc In Application.Documents
doc.Close savechanges:=wdDoNotSaveChanges
Next doc
If Documents.Count > 0 Then
Documents.Close savechanges:=wdDoNotSaveChanges
Else
End If
End Sub

Monday, June 6, 2011

Close without saving changes

Do you ever get partway through changing a Word document and think "Oh this is totally silly"?
Here's the macro to shut down what you're doing and abandon all changes.
Effortlessly.
Without getting confused over Word's skill-testing questions late at night.
Public Sub CloseNoSave()
''' Close the current document without saving it
ActiveDocument.Close (wdDoNotSaveChanges)
End Sub

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