MajP
You've got your good things, and you've got mine.
- Local time
- Yesterday, 23:05
- Joined
- May 21, 2018
- Messages
- 9,067
As a Class there are some problems in structure.
1. The way it is set up now is really slow. You are creating an instance of your class on every call to the before update. Create it once and then use it for the life of the form. You could do this on the first before update, but it takes time for Word to load in the background. I would just do it on form load and after that it is very fast.
2. You pass in a form instance to your spelling check but do not use it in your code. No need for it.
3. I modified the function to return a list of words that are misSpelled. This gives the user a chance to determine if they want to spell check or not
Then I relied on the built in spell checker.
In your class you have a property Application you should set it once, and then just call it. You seem to do this multiple times even using local instances. You are defeating the purpose of a class the way this is structured
Trying to run the spell check in the before update not possible. See if this version using some of the class and the built in spell checker makes sense. Gives you a little of both. It is not truly before update, but provides much more flexibility
1. It alerts you if there are potential errors.
2. Allows you to decide if you want to spell check
3. Runs real spell check.
I would likely stick with using the built in spell checker. I always find these "unseen" automation instances to get hung up and the next thing you know you have 20 instances of Word running in the background. The first example uses a modified version of the word class, the second example uses just built in spelling.
1. The way it is set up now is really slow. You are creating an instance of your class on every call to the before update. Create it once and then use it for the life of the form. You could do this on the first before update, but it takes time for Word to load in the background. I would just do it on form load and after that it is very fast.
2. You pass in a form instance to your spelling check but do not use it in your code. No need for it.
3. I modified the function to return a list of words that are misSpelled. This gives the user a chance to determine if they want to spell check or not
Then I relied on the built in spell checker.
Code:
Private Sub ErrorDescription_BeforeUpdate(Cancel As Integer)
'1. This is a lot of overhead to create a whole new instance of WDSC on every before update. Works best to set it once in the form load
' Set WDSC = New WDSpellCheck
If WDSC Is Nothing Then Set WDSC = New WDSpellCheck
'2. Do not need to pass the form since not used
Dim MisSpelledWords As String
MisSpelledWords = WDSC.MySpellCheck(Nz(Me.ActiveControl.OldValue, vbNullString), Nz(Me.ActiveControl.Value, vbNullString))
If MisSpelledWords <> "" Then
Dim rtn As String
rtn = MsgBox("The following words were potentially mispelled:" & vbCrLf & vbCrLf & MisSpelledWords & vbCrLf & vbCrLf & "If you would like to spell check these then select YES.", vbYesNo, "Mispelled")
If rtn = vbYes Then
RunSpellCheck = True
Else
RunSpellCheck = False
End If
End If
End Sub
In your class you have a property Application you should set it once, and then just call it. You seem to do this multiple times even using local instances. You are defeating the purpose of a class the way this is structured
Code:
Public Function MySpellCheck(strToCheckOld As String, StrToCheckNew As String) As String
'1. You pass in the form and it is unused
' MySpellCheck(frm As Access.Form, strToCheckOld As String, StrToCheckNew As String) As Boolean
'2. You create a local variable with the same name as the class variable and then create another instance of WDSpellCheck
'Dim WDSPC As WDSpellCheck
Dim aryStringtoCheck() As String
Dim intAryIndex As Integer
varStatusWait = SysCmd(acSysCmdSetStatus, "Invoking Your Custom Spellchecker. Please wait....")
'3. The below defeats the purpose of a class module
'Set WDSPC = New WDSpellCheck
'4. WHY pass in the Form? Unused.
' With frm
If Nz(strToCheckOld, vbNullString) <> Nz(StrToCheckNew, vbNullString) Then
aryStringtoCheck() = Split(Nz(StrToCheckNew, vbNullString), " ")
mySpellCheck = ""
For intAryIndex = 0 To UBound(aryStringtoCheck)
If Me.Application.checkspelling(aryStringtoCheck(intAryIndex)) = False Then '5. Use Me.application
'True means error
'pass back the word
MySpellCheck = MySpellCheck & aryStringtoCheck(intAryIndex) & vbCrLf
'If MsgBox("""" & aryStringtoCheck(intAryIndex) & """" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "is not spelled correctly." & Chr(13) & Chr(10) & _
' "Correct the spelling or add the misspelled word to the dictionary.", vbYesNo, "Change or Add Word") = vbYes Then
' Call AddToDict(aryStringtoCheck(intAryIndex))
'End If
End If
Next intAryIndex
End If
' End With
varStatusWait = SysCmd(acSysCmdClearStatus)
End Function
Trying to run the spell check in the before update not possible. See if this version using some of the class and the built in spell checker makes sense. Gives you a little of both. It is not truly before update, but provides much more flexibility
1. It alerts you if there are potential errors.
2. Allows you to decide if you want to spell check
3. Runs real spell check.
I would likely stick with using the built in spell checker. I always find these "unseen" automation instances to get hung up and the next thing you know you have 20 instances of Word running in the background. The first example uses a modified version of the word class, the second example uses just built in spelling.