![]() |
|
|
|
#1
|
|||
|
|||
|
Added Error Handling as sometimes the temp file doesn't close and added a variable to maintain the focus on the original document:
Code:
Private Sub cmdSpellCheck_Click()
'Spell check certain fields in a UserForm
'Adapted from http://computer-programming-forum.com/1-vba/3853759c12e8d3ae.htm
'By Ann K Bohma, Bra Utbildning AB, Sweden
Dim sActiveDoc As String
sActiveDoc = ActiveDocument.Name 'maintain focus on the active document
'Create a new document
Dim oDoc2 As Document
Set oDoc2 = Documents.Add
On Error Resume Next
With oDoc2
'Set language and style (can be omitted)
'.Styles(wdStyleNormal).LanguageID = wdEnglish
'.Range.Style = wdStyleNormal
DoSpellCheck TextBox1
'etc etc or Loop all controls checking for textcontrols
.SpellingChecked = True 'can be omitted,
.Close wdDoNotSaveChanges
End With
Documents(sActiveDoc).Activate
MsgBox "Spelling and Grammar Check Complete"
End Sub
Private Sub DoSpellCheck(oTxtBox As Control)
Dim oRng As Range
Dim strText As String
With oTxtBox
If Not .Value = "" And Not .Value = "<WhatEver>" Then
Set oRng = oDoc2.Range
oRng = .Value
With oRng
.CheckSpelling AlwaysSuggest:=True
.CheckGrammar 'I added grammar to the code from the post
'Set the range eliminating the last CR/Enter
.SetRange .Start, .End - 1
End With
'Return data to the Userform
.Value = oRng.Text
End If
End With
End Sub
|
|
#2
|
|||
|
|||
|
I have been using this code successfully with the exception of the temporary document created. It will not close. Any help would be much appreciated.
Code:
Private Sub cmdSpellCheck_Click()
'Spell check certain fields in a UserForm
'Adapted from http://computer-programming-forum.com/1-vba/3853759c12e8d3ae.htm
'By Ann K Bohma, Bra Utbildning AB, Sweden
'Create a new document
Dim sActiveDoc As String
sActiveDoc = ActiveDocument.Name
On Error Resume Next
Dim oDoc2 As Document
Set oDoc2 = Documents.Add
With oDoc2
DoSpellCheck TextBox1
'etc etc or Loop all controls checking for textcontrols
.SpellingChecked = True 'can be omitted,
MsgBox "Spelling and Grammar Check Complete"
.Close SaveChanges:=wdDoNotSaveChanges
End With
lbl_Exit:
Exit Sub
Set oDoc2 = Nothing
End Sub
Public Sub DoSpellCheck(oTxtBox As Control)
Dim oRng As Range
Dim strText As String
With oTxtBox
If Not .Value = "" And Not .Value = "<WhatEver>" Then
Set oRng = ActiveDocument.Range
oRng = .Value
With oRng
.CheckSpelling AlwaysSuggest:=True
.CheckGrammar
'Set the range eliminating the last CR/Enter
.SetRange .Start, .End - 1
End With
'Return data to the Userform
.Value = oRng.Text
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End If
End With
Exit Sub
End Sub
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
UserForm textbox exit event activated with navigation buttons...why? help?
|
orozvik@yahoo.com | Excel Programming | 2 | 05-08-2015 02:50 AM |
| Can't use RTF Textbox in Userform (Word2007) | dherr | Word VBA | 2 | 03-16-2015 07:50 AM |
Userform VBA Textbox Calculation
|
MarkAn | Word VBA | 2 | 08-15-2014 06:50 AM |
Number format in Textbox on userform
|
officeclerk | Excel Programming | 2 | 04-17-2012 01:23 AM |
| Spell Check | WorkerB | Word | 2 | 11-21-2009 07:22 AM |