Friday, September 30, 2011

Detect Duplicate words in sentences

This little macro detects duplicated words in sentences.
unlike Microsoft Word's grammar/spell checker which merely identifies consecutive duplicate words, this macro (and associated function) marks in RED second and subsequent occurrences of words in each sentence.
A short list of noise words is provided as a string constant; we don't mind those words being repeated.
Public Const strcIgnoreWords As String = vbTab & "and" & vbTab & "on" & vbTab & "the" & vbTab
Sub DuplicateWordsInSentences()
Dim prg As Paragraph
For Each prg In ActiveDocument.Paragraphs
Dim snt As Range
For Each snt In prg.Range.Sentences
Call FindDupInSentence(snt)
Next snt
Next prg
End Sub
Function FindDupInSentence(snt As Range)
Dim strAllWords As String
strAllWords = vbTab
Dim wd As Range
For Each wd In snt.Words
Dim strWd As String
strWd = Trim(wd.Text)
If Len(strWd) > 1 Then
If InStr(1, strcIgnoreWords, vbTab & strWd & vbTab) > 0 Then ' we ignore this word
Else
If InStr(1, strAllWords, vbTab & strWd & vbTab) > 0 Then
wd.Font.Color = wdColorRed
Else
strAllWords = strAllWords & strWd & vbTab
End If
End If
Else ' we ignore words of length 1 character
End If
Next wd
End Function

Monday, September 19, 2011

File Diversion

Your new system came in today, and while you are all free to read Word documents from the old server, you are all supposed to save files to the new server on drive J:.
Here's some stripped-down proof-of-concept code:
Public Const strcNewDrive As String = "J:"
Sub FileSave()
MsgBox "FileSave"
If Len(ActiveDocument.Path) = 0 Then ' not previously saved
Call SaveAsDialog(strcNewDrive, ActiveDocument.Name)
Else
If UCase(Left(ActiveDocument.Path, 2)) = strcNewDrive Then ' User is logged on to th drive
ActiveDocument.Save
Else
Call SaveAsDialog(strcNewDrive, ActiveDocument.Name)
End If
End If
End Sub
Sub FileSaveAs()
MsgBox "FileSaveAs"
Call SaveAsDialog(strcNewDrive, ActiveDocument.Name)
End Sub
Function SaveAsDialog(strDrive As String, strName As String)
With Application.Dialogs(wdDialogFileSaveAs)
.Name = strDrive & "\" & strName
.Format = wdFormatDocument
.Show
End With
End Function

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

Tuesday, July 26, 2011

ToggleShowAll

I assign this cute trick to the Shift-Ctrl-8 keyboard shortcut.
It flip-flops between showing everything that's going on in my document, to showing nothing but pristine text and graphics.


Public Sub ToggleShowAll()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Function: ToggleShowAll
'''
''' Comments: Use the current "Show Fields" setting (True or False) to toggle all visibility settings.
'''
''' Arguments: None.
'''
''' Returns: None
'''
''' Date Developer Action
''' --------------------------------------------------------------------------
''' 2006/03/21 Chris Greaves Created
'''
''' The setting ShowFieldCodes will be used to control the settings of ShowFieldCodes, ShowAll , ShowFieldCodes , ShowHiddenText , ShowTabs , ShowSpaces , ShowParagraphs and ShowBookmarks.
''' All flags will be set to the opposite of the current setting of ShowFieldCodes.
''' If ShowFieldCodes is ON, all flags will be set OFF.
''' If ShowFieldCodes is OFF, all flags will be set ON.
''' The new setting will be saved as the key "Show All" in the Application Environment file.
If Documents.Count > 0 Then
Dim boolSet As Boolean
boolSet = Not ActiveWindow.View.ShowFieldCodes
With ActiveWindow.View
.ShowAll = boolSet
.ShowFieldCodes = boolSet
.ShowHiddenText = boolSet
.ShowTabs = boolSet
.ShowSpaces = boolSet
.ShowParagraphs = boolSet
.ShowBookmarks = boolSet
End With
Dim strSet As String
If boolSet Then
strSet = "Y"
Else
strSet = "N"
End If
Else
End If
End Sub