Spell checking

F

Fuzzhead

Is there a way to run spell check in my forms text fields before I save it to
my
document? I looked at the RunSpellcheck macro but it looks like it is made
to run in my document after my form updates its.
 
G

Greg Maxey

I am not sure. Did you look at the suggestion I had for you in you Edit
document post yesterday?
 
G

Greg Maxey

Fuzzhead,

This might work:

rivate Sub CheckSpelling(objTextBox As Control)
Dim oScratchPad As Word.Document
Set oScratchPad = ActiveDocument
Dim oRng As Range
Dim strText As String
With objTextBox
If Not .Value = "" And Not .Value = "<WhatEver>" Then
Set oRng = oScratchPad.Range
oRng = .Value
With oRng
.CheckSpelling AlwaysSuggest:=True
'Clip the end of document marker
.End = .End - 1
End With
.Value = oRng.Text
End If
End With
End Sub

Private Sub CommandButton1_Click()
Dim oScratchPad As Word.Document
Dim oCtr As Control
Set oScratchPad = Documents.Add
With oScratchPad
For Each oCtr In Me.Controls
If TypeOf oCtr Is MSForms.textBox Then
CheckSpelling oCtr
End If
Next oCtr
.SpellingChecked = True
.Close wdDoNotSaveChanges
End With
MsgBox "Check complete"
End Sub
 
F

Fuzzhead

Greg,

It gave me the following error:
Run-time error '4198': Commend faild
at
..Close wdDoNotSaveChanges

It does correct the spelling in the form though.

Fuzzhead
 
G

Greg Maxey

Hmm. I tested it both on Word2003 and Word2007. What version are you
using?

Maybe you could try:

oScratchPad.Close SaveChanges:=wdDoNotSaveChanges

or highlight "Close" and press F1 to see what options you have.
 
F

Fuzzhead

I'm using Word 2003.

I tried 'oScratchPad.Close SaveChanges:=wdDoNotSaveChanges' but still get
the same error.
 
G

Greg Maxey

I am perplexed. I can't see why your editor is throwing that error. What
happens when you try:

Sub Test()
Dim oScratchPad As Word.Document
Dim oRng As Word.Range
'Open a new document to serve as a scratchpad
Set oScratchPad = Documents.Add
Set oRng = oScratchPad.Range
'Write the TextBox content to the scratchpad
oRng = "Test, tist, test"
'Check and correct spelling errors
With oRng
With Dialogs(wdDialogToolsSpellingAndGrammar)
'If user cancels
If .Show = 0 Then
MsgBox "User canceled"
End If
End With
'Clip the end of document marker
.End = .End - 1
End With
oScratchPad.Close SaveChanges:=wdDoNotSaveChanges
End Sub

or just:

Sub Test2()
Dim oScratchPad As Word.Document
Dim oRng As Word.Range
'Open a new document to serve as a scratchpad
Set oScratchPad = Documents.Add
Set oRng = oScratchPad.Range
'Write the TextBox content to the scratchpad
oRng = "Test, tist, test"
MsgBox oRng.Text
oScratchPad.Close SaveChanges:=wdDoNotSaveChanges
End Sub
 
G

Greg Maxey

I just went to a friends computer, downloaded the code from the website
and it ran without error.

What happens if you take Test1 code and make it the command button
click event code in your form? If that runs successfully then you
might trying adding in other relevent parts of the code to see if you
can determine what is making it fail.

I don't know what to tell you. I posted problem in a different forum
and I will let you know if I find a solution.
 
F

Fuzzhead

Hi Greg,

I went back and recopied the following into my form and when I'm in VB and
run it it works perfectly. But when I'm in my document and call up the form
and run it, it still gives me the error message at that same place.

Private Sub Test3_Click()
Dim oScratchPad As Word.Document
Dim oCtr As Control
Dim oRng As Word.Range
Dim bChecked As Boolean
'Open a new document to serve as a scratchpad
Set oScratchPad = Documents.Add
'Iterate through all UserForm Controls
For Each oCtr In Me.Controls
'Process TextBox Controls
If TypeOf oCtr Is MSForms.TextBox Then
With oCtr
If .Value = "" Then
bChecked = True
Else
Set oRng = oScratchPad.Range
'Write the TextBox content to the scratchpad
oRng = .Value
'Check and correct spelling errors
With oRng
With Dialogs(wdDialogToolsSpellingAndGrammar)
'If user cancels
If .Show = 0 Then
bChecked = False
Exit For
End If
End With
'Clip the end of document marker
.End = .End - 1
End With
bChecked = True
'Write the corrected text to the TextBox
.Value = oRng.Text
End If
End With
End If
Next oCtr
oScratchPad.Close SaveChanges:=wdDoNotSaveChanges
If bChecked Then
MsgBox "Check complete"
Else
MsgBox "Spell checking was stopped in process."
End If
End Sub
 
F

Fuzzhead

Greg,

It has to be in the way my macro calls up the form. Please take a look at it
to see if you can see something.

Public Sub WC()
Dim frm As frmWC
Dim oFF As FormFields
Dim i As Long
Dim MyDate

MyDate = Date

Set frm = New frmWC
Set oFF = ActiveDocument.FormFields

On Error Resume Next

With ActiveDocument
frm.Text1.Value = .FormFields("Text19").Result
frm.Text2.Value = MyDate
frm.Text3.Value = .FormFields("Text30").Result
frm.Text4.Value = .FormFields("Text21").Result
frm.Text5.Value = .FormFields("Text64").Result
frm.Text6.Value = .FormFields("Text53").Result
frm.Text7.Value = .FormFields("Text48").Result
frm.Cmd1.Value = .FormFields("Text63").Result
frm.Text8.Value = .FormFields("Text58").Result
frm.Text9.Value = .FormFields("Text38").Result

For i = 88 To 90
If oFF("Check" & i).CheckBox.Value = True Then
frm.Controls("Check" & i).Value = True
Else
frm.Controls("Check" & i).Value = False
End If
Next i
End With

frm.Show

If frm.bIsOK Then
With ActiveDocument
ActiveDocument.FormFields("Text19").Result = frm.Text1.Text
ActiveDocument.FormFields("Text47").Result = frm.Text2.Text
ActiveDocument.FormFields("Text30").Result = frm.Text3.Text
ActiveDocument.FormFields("Text21").Result = frm.Text4.Text
ActiveDocument.FormFields("Text64").Result = frm.Text5.Text
ActiveDocument.FormFields("Text53").Result = frm.Text6.Text
ActiveDocument.FormFields("Text48").Result = frm.Text7.Text
ActiveDocument.FormFields("Text63").Result = frm.Cmd1.Value
ActiveDocument.FormFields("Text58").Result = frm.Text8.Text
ActiveDocument.FormFields("Text38").Result = frm.Text9.Text

For i = 88 To 90
If frm.Controls("Check" & i).Value = True Then
oFF("Check" & i).CheckBox.Value = 1
Else
oFF("Check" & i).CheckBox.Value = 0
End If
Next i
.Fields.Update
End With
Else
ActiveDocument.Saved = True
End If
Set frm = Nothing
End Sub
 
G

Greg Maxey

Fuzzhead,

I feel like I am repeating myself here. I already suggested that you take a
lot of that code in your calling macro and move it to the UserForm
Initialize event and a command button click event.
I still don't know what fmr.bIsOK is.

I think you should try the suggestions above and then lets go from there.
 
G

Greg Maxey

Fuzzhead,

I got the following replies from the other forum I mentioned earlier. I
still think you should revise your calling macro code, you might have
success with these:

Don't know why your man would get the error but how about coding

oScratchPad.Saved = True
oScratchPad.Close

instead? See if it works any better.

Which version of Word does the OP have?
This is a known issue for Word 97, 98 and 2000 and was (at least partly)
fixed for Word 2002 via a patch and, later, by SP3. See:
http://support.microsoft.com/kb/836036/en-us

A number of conditions also triggered this error for Word 98 and 2000. A
search on '4198' at:
http://search.support.microsoft.com/search/default.aspx?catalog=LCID=1033&spid=939&query=4198&adv=
lists them.

Let me know how you come out.
 
B

Beth Melton

For starters, ActiveDocument needs to be removed from the beginning of each
of these lines since they are contained in a With structure for
ActiveDocument:

ActiveDocument.FormFields("Text19").Result = frm.Text1.Text
ActiveDocument.FormFields("Text47").Result = frm.Text2.Text
ActiveDocument.FormFields("Text30").Result = frm.Text3.Text
ActiveDocument.FormFields("Text21").Result = frm.Text4.Text
ActiveDocument.FormFields("Text64").Result = frm.Text5.Text
ActiveDocument.FormFields("Text53").Result = frm.Text6.Text
ActiveDocument.FormFields("Text48").Result = frm.Text7.Text
ActiveDocument.FormFields("Text63").Result = frm.Cmd1.Value
ActiveDocument.FormFields("Text58").Result = frm.Text8.Text
ActiveDocument.FormFields("Text38").Result = frm.Text9.Text

What happens if you comment oScratchPad.Close wdDoNotSaveChanges? Do you
encounter another runtime error? If not then take a look at the document
that should have been closed by the macro and see if anything strikes you as
odd.

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
F

Fuzzhead

Hi Greg,

If I comment oScratchPad.Close wdDoNotSaveChanges it runs fine.

I tried the other way to close and got the same error. Is there a way I can
send this file to you to look at?

Larry
 
G

Greg Maxey

Fuzzhead,

Sure. Provided you accept the fact that it may not look the same if I send
it back ;-).

You should be able to decipher my real e-mail address from the one in the
profile. The big letters don't belong.
 
T

Tony Jollans

I'm intrigued. By "works fine" do you mean it doesn't leave the scratchpad
document open?
 
B

Beth Melton

If that's the case then I suspect you have an ActiveDocument.Close command
that is closing the scratchpad instead of what you expect the ActiveDocument
to be when it hits that line of code. Since the scratchpad is no longer open
then that would be a good reason for encountering the runtime error - Word
can't close what isn't open.

Good catch, Tony - I interpreted "runs fine" as "didn't encounter an error".
:)

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
G

Greg Maxey

That's odd. In the file you sent me scratchpad doesn't close. I still
haven't sorted out why the problem occurs, but oScratchpad.Close generates
the error if the original document is protected. Weird.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top