Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2018, 04:10 PM
rjmiller rjmiller is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2013
Novice
Spell Check TextBox in UserForm
 
Join Date: Jun 2018
Posts: 4
rjmiller is on a distinguished road
Default Spell Check TextBox in UserForm

I've been searching other forums and posts looking for a suitable vba example to spell check text within a userform textbox. I located a post from 2003 that had the most effective solution (I had to make one modification to get it to work):



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 oDoc2 As Document
    Set oDoc2 = Documents.Add
    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

    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

Last edited by rjmiller; 06-13-2018 at 01:45 PM. Reason: Changed "Set oRng = ActiveDocument.Range" to "oDoc2.Range" - as suggested by Guessed
Reply With Quote
  #2  
Old 06-12-2018, 09:27 PM
Guessed's Avatar
Guessed Guessed is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Since you created a document variable as part of the parent sub, you should have passed that document to the DoSpellCheck so you wouldn't need to use ActiveDocument instead.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 06-15-2018, 04:00 AM
rjmiller rjmiller is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2013
Novice
Spell Check TextBox in UserForm
 
Join Date: Jun 2018
Posts: 4
rjmiller is on a distinguished road
Default Error Handling and Document Focus

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
Reply With Quote
  #4  
Old 12-15-2018, 06:42 PM
rjmiller rjmiller is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2013
Novice
Spell Check TextBox in UserForm
 
Join Date: Jun 2018
Posts: 4
rjmiller is on a distinguished road
Default Closing the temp document

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
Reply With Quote
  #5  
Old 12-15-2018, 10:18 PM
gmayor's Avatar
gmayor gmayor is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Undoubtedly by using activedocument rather than the document names you have applied, the code is losing track of which document is which. The folllowing should address it:


Code:
Option Explicit

Private Sub cmdSpellCheck_Click()
    DoSpellCheck TextBox1
    MsgBox "Spelling and Grammar Check Complete"
lbl_Exit:
    Exit Sub
End Sub

Public Sub DoSpellCheck(oTxtBox As Control)
Dim oDoc2 As Document
Dim oRng As Range
    Set oDoc2 = Documents.Add
    With oTxtBox
        If Not .value = "" And Not .value = "<WhatEver>" Then
            Set oRng = oDoc2.Range
            oRng = .value
            With oRng
                .SpellingChecked = False
                .CheckSpelling AlwaysSuggest:=True
                .CheckGrammar
                .End = .End - 1
            End With
            'Return data to the Userform
            .value = oRng.Text
            oDoc2.Close SaveChanges:=wdDoNotSaveChanges
        End If
    End With
    Set oDoc2 = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 12-16-2018, 10:14 AM
rjmiller rjmiller is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2013
Novice
Spell Check TextBox in UserForm
 
Join Date: Jun 2018
Posts: 4
rjmiller is on a distinguished road
Default

Thank you for your response. I receive a run-time error 4198 . If I comment out the "SaveChanges:=wdDoNotSaveChanges" I still get the run-time error. In either case I can leave the file open in the background and the user can close it after they are finished with the primary doc. I'd prefer that the end user have no idea that the temp file even exists and that it just closes. Thank you for simplifying the code. It makes it more efficient for reuse through out the document( I have three userforms on average that I spell check within the doc). If you have any other suggestion I'd appreciate it. Thanks
Reply With Quote
  #7  
Old 12-17-2018, 09:32 PM
Guessed's Avatar
Guessed Guessed is offline Spell Check TextBox in UserForm Windows 10 Spell Check TextBox in UserForm Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try it with a couple of little changes
Code:
Option Explicit
Private Sub cmdSpellCheck_Click()
    DoSpellCheck TextBox1
    MsgBox "Spelling and Grammar Check Complete"
lbl_Exit:
    Exit Sub
End Sub

Public Sub DoSpellCheck(oTxtBox As Control)
Dim oDoc2 As Document, oRng As Range
    With oTxtBox
        If Not .value = "" And Not .value = "<WhatEver>" Then
            Set oDoc2 = Documents.Add
            Set oRng = oDoc2.Range
            oRng = .value
            With oRng
                .SpellingChecked = False
                .CheckSpelling AlwaysSuggest:=True
                .CheckGrammar
                .End = .End - 1
            End With
            'Return data to the Userform
            .value = oRng.Text
            oDoc2.Saved = True
            oDoc2.Close SaveChanges:=wdDoNotSaveChanges
            Set oDoc2 = Nothing
        End If
    End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Spell Check TextBox in UserForm 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
Spell Check TextBox in UserForm Userform VBA Textbox Calculation MarkAn Word VBA 2 08-15-2014 06:50 AM
Spell Check TextBox in UserForm 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:13 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft