• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 587
  • Last Modified:

Add new grammer rules


Anyone know of a way to add new grammer rules into Microsoft Word 2007.

If not explicitly through the software, perhaps some vba code?

  • 5
  • 3
  • 3
2 Solutions
Joanne M. OrzechManager, Document Services CenterCommented:
Not sure exactly what you mean by new grammar rules.... there are many customizations available under Word Options > Proofing > Settings (next to Writing Style dialog) > many options to have one space or two after the period, force comma after last list item, etc.

James_h1023Author Commented:
Yeh thanks I've gone in their and checked the ones I want.  I was hoping I could add in new rules to lets say check the position of apostrophes, and commas?

Lets say for example, a box appearing suggesting placing a comma before "and" or "but".
Joanne M. OrzechManager, Document Services CenterCommented:
Oh - I see - I don't believe there's any such functionality currently built into Word 2007.  As you said, perhaps a macro can do it.   See what the other experts think.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

James_h1023Author Commented:
Excellent thanks - I see what else is suggested :)
Eric FletcherCommented:
I agree with JOrzech about using macros to handle other grammar rules. However, a simple macro can't easily step you through each instance as is done with the grammar checker. Of course, you can set up a macro to actually make the changes with find and replace -- i.e. force a comma before each "but" -- but you might want to have it just highlight the instances so you can review each one. (Consider the 2nd "but" used in the previous sentence for example.)

The wildcard options in Word's Find and Replace can manage some quite sophisticated patterns, and can be helpful in creating macros to flag or correct grammar preferences. For example, you can set commas before "and" in lists by using the following pattern:

Find what: (<[A-z]{2,}, [A-z]{2,})( and)
Replace with: \1, and
Use Wildcards checked on

Here's what you would get if this was applied to content that included the following sentence:

Before: "The list included Fred, Judy, Alan and Sarah."
After: "The list included Fred, Judy, Alan, and Sarah."

The wildcard pattern is looking for two patterns occurring together: the start of a word of 2 or more characters followed by a comma, a space, and another word of 2 or more characters; followed by " and". The two patterns are defined by being enclosed in parentheses so you can then treat them separately in the replace. When found, it replaces what was found with the 1st pattern (/1) plus ", and" to add the comma before the "and".

You could add a format to the Replace with part to make the changes visible: adding Font color: Red would set each change in red so you could later use Find=Font color: Red to jump through the document to confirm each instance of the changes made.

If you can define the rule clearly, you should be able to come up with a macro to use pattens to identify -- and possibly -- correct it.
Joanne M. OrzechManager, Document Services CenterCommented:
Thank you Eric for your input.  Just as an aside thought, the example you use of forcing the comma before the last item in a list can already be set in Word 2007.  
James_h1023Author Commented:
Excellent - thanks for suggestion, I shall give this a go with a couple and get back to you.
Eric FletcherCommented:
Yes, thanks Joanne. I thought I should have confirmed that -- but only after I'd pasted it in and pressed submit! I had copied the example from an old document I maintain about tips for wildcards.

Note too, that in the 3rd last ¶, the pattern designator should be "\1" and not "/1".

To make up for it, here are a couple of other wildcard patterns I find useful and have incorporated into my cleanup routines.

'-- change thousands separator to a fixed space (i.e. 1,234 becomes 1 234):
        .Text = "([0-9])(,)([0-9]{3,})"
        .Replacement.Text = "\1^s\3"
        .MatchWildcards = True

'-- change number hyphen number to number en number
        .Text = "([0-9])(-)([0-9])"
        .Replacement.Text = "\1^0150\3"
        .MatchWildcards = True
James_h1023Author Commented:
Hmm i'm thinking I shall make macro as suggested to keep go through and check a series of 'rules' highlighting appropriately.

I'm having trouble producing the text though.

Any other examples you can give me?
Eric FletcherCommented:
Well, here's quite an old macro I use a lot. I set it up originally in 1995 so it still has a lot of WordBasic but it does still work. I have it set up in a Module named cleanText within a cleanup template that has a bunch of other tools. You should be able to copy the code into a Module from the VB application (Alt-F11, then navigate to Modules on the left under the template name, then right-click to Insert a new Module; then paste the code into it).

This routine fixes a number of common word processing irritants: extra spaces and returns; fixed spaces; different dashes; typographic quotes, etc. I set it up to handle English, French, or Spanish documents, so depending on your selection in the dialog, it will do specific tasks for the different languages. (In Spanish, for example, it can propose to add the ¿ and ¡ characters to the beginnings of questions or exclamations if it finds any.)

If you have text selected when you run it, the macro will only act on the selected text; otherwise it will operate on the whole document. Note that it won't work if you start it at the end of a document.

I hope this gives you some ideas for how you might approach the subject.

I keep meaning to update this for VBA but it is one of those projects that just end up sliding down the list since it still works fine. ;-)
Option Explicit
'*** Global: cleanText  Eric Fletcher (Word 6 or 7)
'*** Removes common keyboarding mistakes (Eng, Fr or Sp) to prepare them for WinWord formatting. 96/11
'-- updated Aug 96 to use pattern matching for multiples
'-- updated Sept 19/96 to fix extra space problem around em dashes, add Spanish in dialog
'-- copied into VBA for cleanup and improvements 98-10-01
'-- updated to IDRC French standards 99-03-07
Dim selFlag As Integer
Dim idName As String
Public Sub MAIN()
Dim quoteChar, msgTxt, explTxt, qmarks, xmarks As String
Dim dr, sq, fq, fx, d As Integer
Dim spq As Object
selFlag = 0
quoteChar = Chr(34)   '> use quoteChar variable as the quote character 034; i.e. for [^p"], use ["^p"+quoteChar]
idName = ""
'--On Error Goto bye
idName = "cleanText... by Eric Fletcher"
If WordBasic.AtEndOfDocument() Then
    msgTxt = "The macro will not run from the end of the document. Either select some text or move to the beginning of the document."
    WordBasic.MsgBox msgTxt, idName, 0
    GoTo bye
End If
WordBasic.EditBookmark Name:="ctSelection", Add:=1      '--mark starting location
'-- Test for a selection; if none, do the whole document.
If Len(WordBasic.[Selection$]()) > 1 Then selFlag = -1 Else selFlag = 0
msgTxt = "This macro will make changes to the document by correcting common keyboarding mistakes."
msgTxt = msgTxt + " You cannot undo this operation so be sure to have a saved copy." + Chr(10)
msgTxt = msgTxt + Chr(10) + "Choose the correct language to continue or Cancel to avoid making changes."
WordBasic.BeginDialog 453, 169, idName
    WordBasic.PushButton 354, 50, 88, 21, "&English"    'Push #1
    WordBasic.PushButton 354, 78, 88, 21, "&French" 'Push #2
    WordBasic.PushButton 354, 106, 88, 21, "&Spanish"   'Push #3
    WordBasic.PushButton 354, 8, 88, 21, "&Info"    'Push #4
    WordBasic.CancelButton 354, 140, 88, 21
    WordBasic.Text 10, 6, 325, 121, msgTxt  'box explanation
    If selFlag Then
        WordBasic.Text 10, 145, 328, 13, "Changes will be made within selection text only.", "Text1"
    End If
Dim dlg As Object: Set dlg = WordBasic.CurValues.UserDialog
dr = WordBasic.Dialog.UserDialog(dlg)
If dr = 0 Then GoTo bye   '> quit if they cancelled
If dr = 4 Then  '-- they asked for info
    GoTo reDisplay
End If
WordBasic.ScreenUpdating 0: WordBasic.WaitCursor 1
'A. **** Common changes (English, French and Spanish)
If Not selFlag Then WordBasic.StartOfDocument   '> start at the beginning of the file if not in a selection
cleanReplace " :", ": ", 0, 0   '> Remove preceding space before : and ;
cleanReplace " ;", "; ", 0, 0   '> (the extra space after will ensure there is 1 space after)
cleanReplace "--", "", 0, 0   '> Double hyphens to em dash
cleanReplace " - ", "  ", 0, 0   '> space hyphen space to space em space
cleanReplace "", "^s ", 0, 0   '> fixed space before em dash
cleanReplace " ^s", "^s", 0, 0   '> remove any space before fixed spaces
'> set quotes and apostrophes to correct typographic marks
Dim tac As Object: Set tac = WordBasic.DialogRecord.ToolsAutoCorrect(False) '> turns on smartquotes to be able to change them all at once
WordBasic.CurValues.ToolsAutoCorrect tac
sq = tac.SmartQuotes
If sq = 0 Then WordBasic.ToolsAutoCorrect SmartQuotes:=1
cleanReplace Chr(34), Chr(34), 0, 0   '> corrects " to  and 
cleanReplace "'", "'", 0, 0   '> corrects ' to  and 
If sq = 0 Then WordBasic.ToolsAutoCorrect SmartQuotes:=0    '> restores smartquote setting (if not on)
'> Removing multiples
cleanReplace "^s{2,}", "^s", 1, 0   '> multiple fixed spaces to  1 fixed space
cleanReplace " {2,}", " ", 1, 0   '> multiple spaces to  1 space
cleanReplace "^t{2,}", "^t", 1, 0   '> multiple tabs to  1 tab
cleanReplace "^l{2,}", "^l", 1, 0   '> multiple newlines to  1 newline
'-- skip over if within footnotes (cannot change ^p in footnote windows)
If WordBasic.SelInfo(25) Then GoTo bypass
cleanReplace "^p^p^p", "^p", 0, 0   '> multiple returns to  1 return
cleanReplace "^p^p", "^p", 0, 0
cleanReplace "^p^p", "^p", 0, 0
cleanReplace "^p^p", "^p", 0, 0
cleanReplace "^t^p", "^p", 0, 0     '> Remove trailing tab at end of paragraph
cleanReplace " ^p", "^p", 0, 0  '> Remove trailing space at end of paragraph
cleanReplace "..^p", ".^p", 0, 0 '> Remove double periods at paragraph ends
cleanReplace "^p ", "^p", 0, 0  '> Remove space at start of new paragraph
cleanReplace "^p^t", "^p", 0, 0   '> Remove tabs at beginning of paragraphs
cleanReplace """, ""^t", 0, 0   '> add tab after bullet
cleanReplace " ^t", "^t", 0, 0 '> Remove spaces before tabs
cleanReplace "^t ", "^t", 0, 0 '> Remove spaces after tabs
cleanReplace "...", "&", 0, 0   '> 3 periods to ellipses
If dr = 1 Then
'B. **** English only changes would follow here
ElseIf dr = 2 Then
'C. **** French only changes
    cleanReplace "n°", "nº", 0, 0   '-- change n-degree sign to nº (0186)
'-- change " to chevrons (French quotes)
    cleanReplace Chr(147), "«", 0, 0
    cleanReplace Chr(148), "»", 0, 0
'-- fix spaces before french punctuation
    cleanReplace "«", "«^s", 0, 0
    cleanReplace "»", "^s»", 0, 0
    cleanReplace ":", "^s:", 0, 0
    cleanReplace ";", "^s;", 0, 0
    cleanReplace "(", "(^s", 0, 0
    cleanReplace ")", "^s)", 0, 0
    cleanReplace "?", "^s?", 0, 0
    cleanReplace "!", "^s!", 0, 0
    cleanReplace "%", "^s%", 0, 0
    cleanReplace "$", "^s$", 0, 0
    cleanReplace " ^s", "^s", 0, 0  '-- change space-fixed space to fixed space
    cleanReplace "^s ", "^s", 0, 0  '-- change fixed space-space to fixed space
    cleanReplace "^s^s", "^s", 0, 0     '-- change fixed space-fixed space to fixed space
    cleanReplace "oeu", "Su", 0, 0
    cleanReplace "oeil", "Sil", 0, 0
'D. **** Spanish only changes
'-- Check if there are any ? or ! in the document and offer to ensure that the ¿ and ¡ is included
WordBasic.EditFindClearFormatting: WordBasic.EditFind Find:="^0063", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, Format:=0, Wrap:=0
WordBasic.WW7_EditGoTo Destination:="ctSelection"
If WordBasic.EditFindFound() Then fq = -1
WordBasic.EditFindClearFormatting: WordBasic.EditFind Find:="!", Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, Format:=0, Wrap:=0
WordBasic.WW7_EditGoTo Destination:="ctSelection"
If WordBasic.EditFindFound() Then fx = -1
If fq Or fx Then
    If selFlag Then msgTxt = "This selection" Else msgTxt = "This document"
    msgTxt = msgTxt + " contains at least one '?' or '!'. Spanish questions or exclamations should be preceeded by '¿' and '¡' characters. Do you want this macro to add them to any that do not include them now?"
    explTxt = "Note that this will only add the characters at the beginning of any sentence ending with a '?' or '!' whereas grammatically, it might be better placed at the beginning of the clause. If you choose OK, check them later. (The ¿ is Alt-0191; ¡ is Alt-0161.)"
    WordBasic.BeginDialog 453, 169, "Spanish " + idName
        WordBasic.Text 10, 6, 325, 70, msgTxt       'box explanation 1
        WordBasic.Text 10, 83, 325, 70, explTxt      'box explanation 2
        WordBasic.PushButton 354, 8, 88, 21, "&No", "Push2"
        WordBasic.OKButton 354, 36, 88, 21
Set dlg = WordBasic.CurValues.UserDialog
    Set spq = WordBasic.CurValues.UserDialog
    d = WordBasic.Dialog.UserDialog(spq)
If d = -1 Then
qmarks = addMarks("?", "¿")
xmarks = addMarks("!", "¡")
WordBasic.MsgBox "Added" + Str(qmarks) + " '¿' marks (^0191) to questions and" + Str(xmarks) + " '¡' marks (^0161) to exclamations.", "Spanish " + idName, 0 + 64
    End If
End If
'-- any other unique Spanish stuff could be added here
End If
WordBasic.EditFindClearFormatting: WordBasic.EditReplaceClearFormatting
WordBasic.Beep: WordBasic.PrintStatusBar "Cleanup completed. Apply formatting as required and run spell check."
If WordBasic.ExistingBookmark("ctSelection") Then
    WordBasic.WW7_EditGoTo Destination:="ctSelection"
    WordBasic.EditBookmark Name:="ctSelection", Delete:=1
End If
End Sub
' cleanReplace subroutine 
Private Sub cleanReplace(from$, to_$, pmFlag, formatFlag)
WordBasic.EditReplace Find:=from$, Replace:=to_$, WholeWord:=0, MatchCase:=0, PatternMatch:=pmFlag, Format:=formatFlag, ReplaceAll:=1, Wrap:=0
If formatFlag = 1 Then WordBasic.EditReplaceClearFormatting   'resets Replace to clear format
If pmFlag = 1 Then pmFlag = 0   'resets pattern matching if it was on
End Sub
'  function to add ¿ and ¡ to sentences for Spanish 
Private Function addMarks(markend$, markstart$)
Dim n
Dim looptest
If Not selFlag Then WordBasic.StartOfDocument Else WordBasic.WW7_EditGoTo Destination:="ctSelection"
n = 0
WordBasic.EditFind Find:=markend$, Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=0, SoundsLike:=0, Format:=0, Wrap:=0
looptest = 0
While looptest = 0 And WordBasic.EditFindFound()
'==========While EditFindFound()
    n = n + 1
    WordBasic.EditBookmark Name:="spHere", SortBy:=0, Add:=1
    WordBasic.CharLeft 1
'---------Check with EM to determine rules for placement within quotes
'-- Moves right if the sentence starts with an open quote
    If WordBasic.[Selection$]() = Chr(147) Then WordBasic.CharRight
    WordBasic.Insert markstart$
    WordBasic.WW7_EditGoTo Destination:="spHere"
    WordBasic.CharRight 2
    WordBasic.EditFind Find:=markend$, Direction:=0, Wrap:=0
    If Not selFlag Then
        looptest = 0
        If WordBasic.CmpBookmarks("\Sel", "ctSelection") = 8 _
        Or WordBasic.CmpBookmarks("\Sel", "ctSelection") = 6 _
        Or WordBasic.CmpBookmarks("\Sel", "ctSelection") = 10 _
        Then looptest = 0 Else looptest = 1
    End If
If Not selFlag Then WordBasic.StartOfDocument Else WordBasic.WW7_EditGoTo Destination:="ctSelection"
'-- remove multiples in case there were any in there before
WordBasic.EditReplace Find:=markstart$ + "{2,}", Replace:=markstart$, Direction:=0, MatchCase:=0, WholeWord:=0, PatternMatch:=1, SoundsLike:=0, ReplaceAll:=1, Format:=0, Wrap:=0
WordBasic.EditFindClearFormatting: WordBasic.EditReplaceClearFormatting
addMarks = n
End Function
Private Sub infoDialog()
ReDim h__$(38)
h__$(0) = "This macro was prepared by Eric Fletcher,"
h__$(1) = "Chesley House Publications, ejfletcher@gmail.com"
h__$(2) = "It uses a series of search and replace commands to fix common"
h__$(3) = "keyboarding errors. If you are not sure whether the changes"
h__$(4) = "will be acceptable, save first and check it before saving the"
h__$(5) = "changed document."
h__$(6) = " "
h__$(7) = "The macro will remove:"
h__$(8) = "- Space before : and ;"
h__$(9) = "- Multiple fixed and normal spaces"
h__$(10) = "- Multiple tabs (change to single tab)"
h__$(11) = "- Multiple new-line characters to single"
h__$(12) = "- Tabs and spaces from end of paragraphs"
h__$(13) = "- Tabs and spaces from start of paragraphs"
h__$(14) = "- Spaces before and after tabs"
h__$(15) = " "
h__$(16) = "The macro will change:"
h__$(17) = "- Double hyphens to em dashes (fixed-space, em, space)"
h__$(18) = "- Straight quotes to typographic quotes (chevrons in French)"
h__$(19) = "- Three periods to the ellipses character"
h__$(20) = " "
h__$(21) = "The macro ensures that:"
h__$(22) = "- Explicitly numbered paragraphs have tabs after digits-period"
h__$(23) = "- Explicit bullets have a tab after them"
h__$(24) = " "
h__$(25) = "If the French button is chosen:"
h__$(26) = "- Fixed spaces will be added before : ; ! ? % $"
h__$(27) = "- Quote marks will be changed to open and closed chevrons"
h__$(28) = "- oe will be changed to the ligature Alt-0156 (i.e. Su, Sil)"
h__$(29) = " "
h__$(30) = "If the Spanish button is chosen:"
h__$(31) = "- The option to ensure leading ¿ and ¡ characters"
h__$(32) = "to questions and exclamations will be offered if any"
h__$(33) = "exist in the document (or within the selection)."
h__$(34) = " "
h__$(35) = "-------"
h__$(36) = "Note: If the macro is run within the footnote window, any"
h__$(37) = "changes involving paragraph ends will be skipped."
WordBasic.BeginDialog 500, 300, "Information about the cleanText macro"
    WordBasic.OKButton 402, 268, 88, 21
    WordBasic.ListBox 10, 8, 480, 252, h__$(), "ListBox1"
Dim dlx As Object: Set dlx = WordBasic.CurValues.UserDialog
WordBasic.Dialog.UserDialog dlx
End Sub

Open in new window

James_h1023Author Commented:
Sorry rather busy of past.

Thanks shall have a look at this over the weekend :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now