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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using Microsoft Words Spell Check in VB

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I am writing a simple scrabble program for students at my school.
I have this code and want it to just return good or bad.
I want to pass the letters they used to make one word and have Microsoft Words spell check tell me its a word or not. I don't care how to spell it I just want to make sure that when its scored it is actually a word or not. Can this be done in the background without showing Microsoft Word active? Microsoft Word can be running but I don't want it popping up with the dialog box suggesting words like this code does now.

TIA
Code:
      Public Function Spellcheck(TextStr As String) As String
  
           Set Wordapp = CreateObject("Word.Application")
           Wordapp.Documents.Add
           'Dim wordrange As Word.Range
           Set wordrange = Wordapp.ActiveDocument.Range(0, 0)
           wordrange.Text = TextStr
           Wordapp.Visible = True
           Wordapp.Activate
           wordrange.CheckSpelling , , True
           'wordrange.CheckGrammar
           Spellcheck = wordrange.Text
'           wordrange.Text = ""
'           WordApp.Documents.Close (False)
'           WordApp.Quit
'           Set WordApp = Nothing

      End Function


DougP
 
You do not say what you are writing the program in, but it will probably not be possible to stop Word from offering solutions to the CheckGrammar method.

 
And if you are not even using Word, why not just build a file list yourself and use in whatever you are writing in?

 
You can:
Code:
Public Function Spellcheck(TextStr As String) As [!]Boolean[/!]  
' True - TextStr found in the dictionary
' see help file for other CheckSpelling arguments
    Set Wordapp = CreateObject("Word.Application")
    Spellcheck = Wordapp.CheckSpelling(TextStr)
    WordApp.Quit
    Set WordApp = Nothing
End Function

combo
 
combo, it works perfect, thankx

But I guess on the WordApp.Quit line Word came up asking about saving changes to the Document.docm file. that something was using it.

So I just remmed the last two lines out. I need this to check a words all through the time the game is played.

I'll put the WordApp.Quit line in the close of the whole game.
maybe there is something to appease the "saving changes to the Document.docm" error when that happens?
I'm using Word 2007 and we are at school as well.

fumei, this is written in VB6.

DougP
 
something to appease the "saving changes to the Document.docm" error
WordApp.Quit 0 ' 0=wdDoNotSaveChanges

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
>I'll put the WordApp.Quit line in the close of the whole game

Given that this should only come up if Word is open with a dirty document (i.e one that has been modified since it was opened) which in turn means that either the user already had Word opened before your game was started (or, less likely, that they opened Word once your game was in play) you may not want to do this.

You probably want to code more defensively. Check whether Word is already running, and then take the apprpriate actions when exiting. For example, modifying combo's code:
Code:
[blue][green]' True - TextStr found in the dictionary
' see help file for other CheckSpelling arguments[/green]
Public Function Spellcheck(TextStr As String) As Boolean
    Dim WordApp As Object
    Dim NewWord As Boolean
    
    NewWord = False
    Set WordApp = GetObject("", "Word.Application")
    
    If WordApp Is Nothing Then
        NewWord = True
        Set WordApp = CreateObject("Word.Application")
    End If
    
    Spellcheck = WordApp.CheckSpelling(TextStr)
    
    If NewWord Then
        Set WordApp = Nothing
    Else
        [green]' However you want to handle the fact that Word was already open when you hit this function
        ' probably best just leaving it alone[/green]
    End If
End Function[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top