PDA

View Full Version : [SOLVED:] vba word game help



albino_pygmy
12-18-2016, 01:22 PM
I'd like to come up with a word game or two, and would need some help. Is there a way to scan a string through a dictionary to check if it's a real word, or is there a way to take that string and access a website like an online scrabble dictionary?

Logit
12-18-2016, 06:04 PM
This macro will check spelling on all sheets in a workbook by enabling the Windows Spell Checker.
It shouldn't be necessary to use an online dictionary. The challenge now is to determine how you
can utilize the function of the Spell Checker in your project.


Sub SpellChecker()
Dim s As Worksheet
For Each s In Worksheets
s.Cells.CheckSpelling
Next s
End Sub

albino_pygmy
12-18-2016, 07:57 PM
Thank you for your response, but I'm working in PowerPoint, and not Excel. I don't think that's even possible in PowerPoint?

John Wilson
12-19-2016, 04:37 AM
It is possible to activate the Excel Spell Check from PowerPoint but I don't see how it could help. The CheckSpelling method has no return so there is no obvious way of knowing whether the word is correct.

John Wilson
12-19-2016, 05:15 AM
This code would search a text file called dic.txt on your desktop


Sub chex_Word()

' word goes here
If Not Word_exists("ABout") Then
MsgBox "It's not a word I know.", vbInformation
Else
MsgBox "It'a a word I know.", vbInformation
End If
End Sub
Function Word_exists(strSearch As String) As Boolean
Dim strPath As String
strPath = Environ("USERPROFILE") & "\Desktop\dic.txt"
Dim strLine As String
Dim FileNum As Integer
strSearch = LCase(strSearch)
FileNum = FreeFile
Open strPath For Input As #FileNum
Do While Not EOF(FileNum)
Line Input #FileNum, strLine
If InStr(1, strLine, strSearch, vbTextCompare) > 0 Then
Word_exists = True
Exit Do
End If
Loop
Close #FileNum
End Function

You can download a copy of dic.txt from here (http://www.pptalchemy.co.uk/Downloads/dic.zip)

A) It does not contain ALL words
B) I cannot remember where I got it so cannot attribute.

albino_pygmy
12-19-2016, 10:31 PM
I'll give that a shot. Thanks John!

Paul_Hossler
12-20-2016, 08:06 PM
It'd probably be faster to read the list of words one time at the beginning and then just check them as you go




Option Explicit

Dim D As Object

Sub LoadDic()
Dim strPath As String, strLine As String
Dim FileNum As Integer

Set D = CreateObject("Scripting.Dictionary")
strPath = Environ("USERPROFILE") & "\My Documents\dic.txt"

FileNum = FreeFile

Open strPath For Input As #FileNum
Do While Not EOF(FileNum)
Line Input #FileNum, strLine
Call D.Add(strLine, strLine)
Loop
Close #FileNum
End Sub

Function IsWord(s As String) As Boolean
IsWord = D.exists(s)
End Function


Sub drv()
MsgBox IsWord("cat")
MsgBox IsWord("dog")
MsgBox IsWord("zootoxin")

MsgBox IsWord("qqqqqqqq")
End Sub

John Wilson
12-21-2016, 03:43 AM
I was thinking that way too but I was surprised how fast the original code I posted runs.

John

Paul_Hossler
12-21-2016, 07:20 AM
I was thinking that way too but I was surprised how fast the original code I posted runs.

John

Yea it's probably a wash for a few words. But ...

1. Run time is linear function of number of words being checked, 100 words = 100 times as long

2. It's on disc each time which is slower than memory, plus opening and closing takes time

3. Since the file is sorted, once you go past the word you're searching for you can exit the loop

4. Since the file is sorted, you could use a binary search and just narrow in on the word (possibly faster)

Maybe something like this untested tweak



Sub chex_Word()

' word goes here
If Not Word_exists("ABout") Then
MsgBox "It's not a word I know.", vbInformation
Else
MsgBox "It's a word I know.", vbInformation
End If
End Sub


Function Word_exists(strSearch As String) As Boolean
Dim strPath As String
strPath = Environ("USERPROFILE") & "\Desktop\dic.txt"
Dim strLine As String
Dim FileNum As Integer

Word_exists = False

strSearch = LCase(strSearch)

FileNum = FreeFile

Open strPath For Input As #FileNum

Do While Not EOF(FileNum) and strLine < strSearch
Line Input #FileNum, strLine
If strLine = strSearch Then Word_exists = True
Loop

Close #FileNum

End Function

John Wilson
12-21-2016, 08:25 AM
I follow the reasoning but my test txt file was 128,000 word and even checking a word way down the list (waste) it returned an answer in 0.2 sec. I actually thought is would take longer.

Here it is running http://screencast.com/t/S4TxUnXaqg

Paul_Hossler
12-21-2016, 10:56 AM
Yes it is fast and yes I though it'd take longer also

All I was saying is that checking 100 works would take 20 seconds, and that when you find a match, your macro still reads to the end of the file

John Wilson
12-22-2016, 06:19 AM
The Exit Do would jump out of the loop once a match was found

Paul_Hossler
12-22-2016, 07:49 AM
:doh:Sorry - I missed that part

My bad

albino_pygmy
12-24-2016, 02:03 PM
Working great, thanks John and Paul!

KeiranLee
10-13-2021, 11:49 AM
Thanks a lot for this brilliant solution! It might helps me a lot!

KeiranLee
10-15-2021, 11:00 AM
missed thread, sorry mistake