Wednesday, August 31, 2011

Rebuild your Active Document

Your document is corrupt, or it is so heavily edited that it is behaving badly.
Use the little macro "RebuildActiveDocument" to grab all your text, unformatted, and rebuild the document.
CAUTION: make a copy of your document using Windows Explorer before running this macro.

Sub RebuildActiveDocument()

' Get the name of the source ActiveDocumentument
Dim strFilename As String
strFilename = ActiveDocument.FullName
' Get the stoty-content of the ActiveDocumentument
Dim strStoryContent As String
Dim lng As Long
For lng = 1 To ActiveDocument.StoryRanges.Count
strStoryContent = strStoryContent & ActiveDocument.StoryRanges(lng).Text
Next lng
' Close the ActiveDocumentument WITHOUT saving changes
ActiveDocument.Close (wdDoNotSaveChanges)
' Create a new ActiveDocumentument
Documents.Add
' Insert the text file
Selection.TypeText (strStoryContent)
' Save the ActiveDocumentument with the original name
ActiveDocument.SaveAs (strFilename)
End Sub

Thursday, August 18, 2011

Print View, Whole page, All documents

From time to time I find myself reviewing or updating a set of a dozen or so documents.

I like to see them in a consistent manner.

Perhaps they are a set of flyers, and I want to make sure that the layout above-the-fold is identical.

This little macro loops through all open documents, moves the cursor to the top of the document, then chooses Print View (WYSIWYG, almost) and a whole-page on the screen.

Sub PrintViewAll()

Dim doc As Document
For Each doc In Application.Documents
doc.Activate
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.Type = wdPrintView
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
Application.WindowState = wdWindowStateMaximize\
Next doc
End Sub


P.S. If you'd rather be left at the foot of the documents, to append new text, use:-
Selection.EndKey Unit:=wdStory
P.P.S. If you'd rather be left in Normal view, use:
ActiveWindow.View.Type = wdNormalView

Tuesday, August 9, 2011

File,Close (No Save)

I'm feeling guilty about the length of the previous post.
I hope you'll accept this token of remorse.

We use Ctrl-F4 to close a document, and of course if the contents of the document have changed we get to answer the skill-testing question "Do you want to save?".
Oftentimes we know in advance that we don't want the changes. Perhaps we have been experimenting with the document and want to discard all changes before we accidentally overwrite the good version.

Closing a File (document) without saving changes is a one-liner, as shown below.

For extra utility you may want to assign an unused shortcut key combination to this macro.

Sub FileCloseNoSave()

ActiveDocument.Close (wdDoNotSaveChanges)
End Sub

Monday, August 8, 2011

Mark Duplicated Words

If you'd like an easy way to improve your writing style, try this lengthy-looking macro and function.
It examines each sentence in your document looking for repeated words.
A string "strDismiss" is set up to avoid reporting words which I think are allowed to be repeated.
repeated words are flagged with Microsoft Word's character style "Strong", so you may want to modify that in your Normal.dot to be bright pink, 16 point, or whatever.
Don't forget that you can clear all local formatting with Ctrl-A followed by Ctrl-Space.

Public strDismiss As String

Sub MarkDuplicatedWords()
strDismiss = vbTab & "for" & vbTab & "and" & vbTab & "that" & vbTab & "," & vbTab & "." & vbTab & "to" & vbTab
strDismiss = strDismiss & "http" & vbTab & "://" & vbTab & "www" & vbTab & "com" & vbTab & "/" & vbTab & "the" & vbTab
strDismiss = strDismiss & "a" & vbTab & "of" & vbTab & "www" & vbTab & "com" & vbTab & "/" & vbTab & "the" & vbTab
strDismiss = strDismiss & """" & vbTab & "of" & vbTab & "www" & vbTab & "com" & vbTab & "/" & vbTab & "the" & vbTab
Dim prg As Paragraph
For Each prg In ActiveDocument.Paragraphs
Dim snt As Range
For Each snt In prg.Range.Sentences
If lngCountDuplicatedWords(snt, strDismiss, ActiveDocument.Styles("Strong")) > 0 Then
Else
End If
Next snt
Next prg
End Sub
Function lngCountDuplicatedWords(rng As Range, strDismiss As String, sty As Style)
Dim lngResult As Long
Dim strSentence As String
strSentence = vbTab
Dim wd As Range
For Each wd In rng.Words
Dim strWord As String
strWord = Trim(wd.Text)
If InStr(1, strDismiss, vbTab & strWord & vbTab) > 0 Then ' we can ignore this word
Else
If InStr(1, strSentence, vbTab & strWord & vbTab) > 0 Then
lngResult = lngResult + 1
wd.Style = sty
Else
strSentence = strSentence & strWord & vbTab
End If
End If
Next wd
lngCountDuplicatedWords = lngResult
End Function