Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Needing help with spellchecking a textbox 1

Status
Not open for further replies.

AncientTiger

Programmer
Jul 5, 2001
238
0
0
US
Ok, I've researched it and found a method for using a Word process to spellcheck a textboxes contents, however no matter what I try I just CANNOT get it to ignore the letter case. If it's typed in all lowercase, then it works fine, but uppercase it missing spelling errors altogether.

I'm just not that familiar with the methodology and syntax for this, if someone could point me in the right direction?

I also am having an issue with the spelling error window not popping up, but rather staying behind everything else. I have to Alt-Tab to activate it. I've tried appactivate with the "Spelling" title of that screen, but to no avail. Maybe a tip on how to remedy that as well?



Here's the routine that checks the text:
Code:
Public Sub SPELLCHECKER(ByVal FF As Variant)

    Dim objWord As Object
    Dim objDoc  As Object
    Dim strResult As String
    
    'Create a new instance of word Application
    Set objWord = CreateObject("word.Application")
    Set objDoc = objWord.Documents.Add(, , 1, True)

    objDoc.Content = FF.Text
    objDoc.CheckSpelling , , , True
    objWord.Visible = False

    strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    ' Correct the carriage returns
    strResult = Replace(strResult, Chr(13), Chr(13) & Chr(10))
    
    If FF.Text = strResult Then
        ' There were no spelling errors, so give the user a
        ' visual signal that something happened
        MsgBox "The spelling check is complete.", vbInformation + vbOKOnly
    End If
    
    'Clean up
    objDoc.Close False
    Set objDoc = Nothing
    objWord.Application.Quit True
    Set objWord = Nothing

    ' Replace the selected text with the corrected text. It's important that
    ' this be done after the "Clean Up" because otherwise there are problems
    ' with the screen not repainting
    FF.Text = strResult

End Sub

I've attached the sample project that has the issues listed.

Thanks in advance for all your help :D

------------------------------------
[yinyang] Over 30 years of programming, and still learning every day! [yinyang]
 
 https://files.engineering.com/getfile.aspx?folder=4b390d55-d357-4024-a977-3808d5778ccc&file=SpellCheckerExmaple.zip
CheckSpelling has IgnoreUppersase second argument, optional, set it to False.

If you open word and go to VB editor (Alt+F11), you cad dosplay object browser, find Document object and CheckSpelling method with arguments description. For help you can hit F1 after selecting it.

combo
 
Tried that and it didn't work. I've tried multiple variations and I just cannot get it to properly spellcheck uppercase. I'm almost to the point of just Lcase'ing everything, spellchecking it, and then Ucase'ing everything and folks will just have to scream at each other in correspondence. LOL

In my example project, here's what I've tried that failed:
Code:
objDoc.CheckSpelling ,false
objDoc.CheckSpelling ,true
objDoc.CheckSpelling ,,false
objDoc.CheckSpelling ,,true
objDoc.CheckSpelling ,,,false
objDoc.CheckSpelling ,,,true

I did find the documentation you referred to and it would seem that the 2nd argument WOULD be the key to getting it to ignore the case, but again, tried and failed.

Function CheckSpelling(Word As String, [CustomDictionary], [IgnoreUppercase], [MainDictionary], [CustomDictionary2], [CustomDictionary3], [CustomDictionary4], [CustomDictionary5], [CustomDictionary6], [CustomDictionary7], [CustomDictionary8], [CustomDictionary9], [CustomDictionary10]) As Boolean


------------------------------------
[yinyang] Over 30 years of programming, and still learning every day! [yinyang]
 
In this case [tt]Checkspelling(word:="upercase", IgnoreUppercase:=false)[/tt] returns False, it is word function. You don't need to copy text to word to use it.
There is also CheckSpelling for Application, Document and Range (in your posts you try to apply document's method. But "If the document or range contains errors, this method displays the Spelling and Grammar dialog box (Tools menu), with the Check grammar check box cleared. For a document, this method checks all available stories (such as headers, footers, and text boxes)."
Anyway, you can pass named arguments for optional non-default ones.

combo
 
Just to clarify, this project is a Visual Studios 6 project, not a Word VBA script, so yes, I have to create a word object to copy text to to invoke the word spellcheck function.

Unless there's another method for spellchecking textbox contents, I'm stuck with utilizing a word.application object. Any other suggestions would be pure gold in my book, as long as it doesn't involve third party components that I'm not allowed to install on work machines.

------------------------------------
[yinyang] Over 30 years of programming, and still learning every day! [yinyang]
 
You've got at least a couple of issues here.

Firstly, despite the documentation, IgnoreUppercase is in fact ignored. The spellcheck honours Word's Options settings for this. But that's fixable, since we can simply set that through code (e.g. [tt]objWord.Options.IgnoreUppercase = False[/tt])

Sadly trickier is the dialog box. This belongs to Word, not to VB, so unless Word is higher than VB in trhe Zorder, then the dialog will not be - and CreateObject does not allow us to play with zorder. So you need to add both a .Visible = True and .Activate to get the effect you want - with the downside that a Word window will flash visible even if there are no spelling errors.

There are other tricks for this, which in the extreme can get messy(for example, intercepting the low-level creation of the dialog box and changing its parent)

It is a shame that Microsoft dumped their original SAPI (Spelling API) years ago, and that the one reintroduced in Windows 8 is not directly accessible to VB; you can use it, but you need a type library such as the one found here, an update to the trusty old ole library from E Mordano. But it still requires some expert knowledge of OLE and some low-level tricks. there is an example for this provided, so no point me reinventing the wheel. Just to be clear, a type library is not a code library and does not need to be distributed with your application.
 
Strongm, you are mah hero (insert southern accent and a big smacker!)

That worked like a charm. I tweaked the code using your suggestions, and was even able to get the Word dialog spellcheck to pop up, while the word window minimized ;)

Code:
    'Create a new instance of word Application
    Set objWord = CreateObject("word.Application")
    Set objDoc = objWord.Documents.Add(, , 1, True)
    objWord.Options.ignoreuppercase = False
    objWord.Visible = True
    objWord.WindowState = 2
    objWord.Activate
    DoEvents
    objDoc.Content = FF.Text
    objDoc.CheckSpelling , True

Thanks again for the tips!!

------------------------------------
[yinyang] Over 30 years of programming, and still learning every day! [yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top