View Single Post
 
Old 03-31-2013, 01:51 PM
AlexR AlexR is offline Windows XP Office 2007
Novice
 
Join Date: Mar 2013
Location: Eastern PA, USA
Posts: 8
AlexR is on a distinguished road
Default

As often happens, I find that asking the question helps me formulate what my question actually is, and I've come up with a solution.

I don't need to bind the macro to the punctuation, I just need to check to see whether the selection is punctuation and if so, make a new selection to the old selection's left. Here's the code:

Code:
Sub CheckLastWord2()
    Dim errorCount As Object
     ' Insert a space since this macro bound to the spacebar
    Selection.TypeText Text:=" "
     ' Select word just typed including space with cursor to left of word
    Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    'if the selection is 2 characters, assume it is punctuation and move to the left of it
    If Selection.Range.Characters.Count = 2 Then
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    End If
    ' Set errorCount to count errors in selected text
    Set errorCount = Selection.Range.SpellingErrors
     ' move cursor to the right of word just typed
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    If errorCount.Count > 0 Then
        MsgBox "Wrong"
        ' select the word just typed ready for retyping
        Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    'if there are no errors, move the cursor back where it was
    Else: Selection.MoveRight Unit:=wdCharacter, Count:=2
    End If
End Sub
Reply With Quote