Consulting

Results 1 to 16 of 16

Thread: vba word game help

  1. #1

    vba word game help

    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?

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    553
    Location
    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

  3. #3
    Thank you for your response, but I'm working in PowerPoint, and not Excel. I don't think that's even possible in PowerPoint?

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,017
    Location
    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
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,017
    Location
    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

    A) It does not contain ALL words
    B) I cannot remember where I got it so cannot attribute.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    I'll give that a shot. Thanks John!

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Posts
    8,036
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,017
    Location
    I was thinking that way too but I was surprised how fast the original code I posted runs.

    John
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Posts
    8,036
    Location
    Quote Originally Posted by John Wilson View Post
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,017
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Posts
    8,036
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,017
    Location
    The Exit Do would jump out of the loop once a match was found
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Posts
    8,036
    Location
    Sorry - I missed that part

    My bad
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  14. #14
    Working great, thanks John and Paul!

  15. #15
    Banned VBAX Newbie
    Joined
    Oct 2021
    Posts
    4
    Location
    Thanks a lot for this brilliant solution! It might helps me a lot!

  16. #16
    Banned VBAX Newbie
    Joined
    Oct 2021
    Posts
    4
    Location
    missed thread, sorry mistake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •