none
How to activate a programmatically modified custom dictionary RRS feed

  • Question

  • Hi

    I am adding a new word into the active custom dictionary with the following (VB6) code

        Dim l_oDict     As Word.Dictionary
        Dim l_oNewDict  As Word.Dictionary
        Dim l_hFile     As Long
        Dim l_sFile     As String
        
        Set l_oDict = m_oWordApp.CustomDictionaries.ActiveCustomDictionary
        
        l_sFile = l_oDict.Path & "\" & l_oDict.Name
        l_hFile = FreeFile
        Open l_sFile For Append As #l_hFile
        Print #l_hFile, p_sText
        Close #l_hFile
        
        Set l_oNewDict = m_oWordApp.CustomDictionaries.Add(l_sFile)
        m_oWordApp.CustomDictionaries.ActiveCustomDictionary = l_oNewDict

    I am finding that if I restart my WordApp object ("Word.Application"), the new word is recognised and I no longer  get the mis-spelling squiggle. However, I really do want to register the new word on-the-fly. 

    Playing around with Word, I can go and look at the words in the custom dictionaries and, without modifying them, see the word I have added with the above code, exit the custom dictionaries dialog and find that the word is now recognised in Word itself as well. So Word is obviously refreshing the list without having to exit!

    What other steps do I need to take to get my own WordApp to see the modified custom dictionary?

    I am doing all this late bound so was hoping for a version independent (of Word) solution

    Many thx

    S


    • Edited by Simon Woods Wednesday, September 19, 2012 6:39 AM More details
    • Moved by Cindy Meister MVPModerator Wednesday, September 19, 2012 8:39 AM not using VSTO technology question cannot be answered in the context of VSTO (From:Visual Studio Tools for Office)
    Wednesday, September 19, 2012 6:37 AM

Answers

  • OK. So think I've made it work. Maybe doing too much here ... but the methodology I'm using is 

    Private Sub AddToDictionary(ByVal p_sWord As String)
        
        Dim l_cPaths    As VBA.Collection
        Dim l_oDict     As Word.Dictionary
        Dim l_sFile     As String
        
        Set l_cPaths = GetCustomDictionaryPaths
        Set l_oDict = m_oWordApp.CustomDictionaries.ActiveCustomDictionary
        
        l_sFile = l_oDict.Path & "\" & l_oDict.Name
        AddWordToDictionary l_sFile, p_sWord
        
        m_oWordApp.CustomDictionaries.ClearAll
        RestoreCustomDictionaries l_cPaths
        Set l_oDict = m_oWordApp.CustomDictionaries.Add(l_sFile)
        m_oWordApp.CustomDictionaries.ActiveCustomDictionary = l_oDict
            
    End Sub
    
    Private Function GetCustomDictionaryPaths() As VBA.Collection
    
        Dim l_cPaths    As VBA.Collection
        Dim l_oDict     As Word.Dictionary
        
        Set l_cPaths = New VBA.Collection
    
        For Each l_oDict In m_oWordApp.CustomDictionaries
            l_cPaths.Add l_oDict.Path & "\" & l_oDict.Name
        Next
        
        Set GetCustomDictionaryPaths = l_cPaths
        
    End Function
    
    Private Sub AddWordToDictionary(ByVal p_sFile As String, ByVal p_sWord As String)
    
        Dim l_hFile     As Long
           
        l_hFile = FreeFile
        Open p_sFile For Append As #l_hFile
        Print #l_hFile, p_sWord
        Close #l_hFile
    
    End Sub
    
    Private Sub RestoreCustomDictionaries(ByVal p_cDicts As VBA.Collection)
    
        Dim l_vPath As Variant
        
        For Each l_vPath In p_cDicts
            m_oWordApp.CustomDictionaries.Add CStr(l_vPath)
        Next
    
    End Sub

    HTH

    S





    Wednesday, September 19, 2012 9:26 AM

All replies

  • OK. So think I've made it work. Maybe doing too much here ... but the methodology I'm using is 

    Private Sub AddToDictionary(ByVal p_sWord As String)
        
        Dim l_cPaths    As VBA.Collection
        Dim l_oDict     As Word.Dictionary
        Dim l_sFile     As String
        
        Set l_cPaths = GetCustomDictionaryPaths
        Set l_oDict = m_oWordApp.CustomDictionaries.ActiveCustomDictionary
        
        l_sFile = l_oDict.Path & "\" & l_oDict.Name
        AddWordToDictionary l_sFile, p_sWord
        
        m_oWordApp.CustomDictionaries.ClearAll
        RestoreCustomDictionaries l_cPaths
        Set l_oDict = m_oWordApp.CustomDictionaries.Add(l_sFile)
        m_oWordApp.CustomDictionaries.ActiveCustomDictionary = l_oDict
            
    End Sub
    
    Private Function GetCustomDictionaryPaths() As VBA.Collection
    
        Dim l_cPaths    As VBA.Collection
        Dim l_oDict     As Word.Dictionary
        
        Set l_cPaths = New VBA.Collection
    
        For Each l_oDict In m_oWordApp.CustomDictionaries
            l_cPaths.Add l_oDict.Path & "\" & l_oDict.Name
        Next
        
        Set GetCustomDictionaryPaths = l_cPaths
        
    End Function
    
    Private Sub AddWordToDictionary(ByVal p_sFile As String, ByVal p_sWord As String)
    
        Dim l_hFile     As Long
           
        l_hFile = FreeFile
        Open p_sFile For Append As #l_hFile
        Print #l_hFile, p_sWord
        Close #l_hFile
    
    End Sub
    
    Private Sub RestoreCustomDictionaries(ByVal p_cDicts As VBA.Collection)
    
        Dim l_vPath As Variant
        
        For Each l_vPath In p_cDicts
            m_oWordApp.CustomDictionaries.Add CStr(l_vPath)
        Next
    
    End Sub

    HTH

    S





    Wednesday, September 19, 2012 9:26 AM
  • Hi Simon,

    I'm glad to hear you have get work round. Thanks for sharing it in the Forum. It's really useful for other community members who have similar issue to see how you solved it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 20, 2012 9:12 AM
    Moderator