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