View Single Post
 
Old 12-15-2018, 06:42 PM
rjmiller rjmiller is offline Windows 10 Office 2013
Novice
 
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