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