Stoil Pankov

I am trying to use the MS Word spell check in my application. It is fine for words witch are in the main dictionary. For my functionality to be complete I need to add the option of adding words to the custom dictionary so that they are not constantly shown as mistakes.

I am opening the .CustomDictionaries.ActiveCustomDictionary file and adding the new word but at that moment MS Word deselects that dictionary for use. It is possible to reselect it again by going in the Options menu in MS Word but that is annoying.

So how am I supposed to add words to the MS Word Custom dictionary

Best regards

Stoil Pankov

Private Function AddToCustomDictionary(ByVal wordToAdd As String) As Boolean
Try
Dim customDictionaryFile As System.IO.FileStream
Dim filePath As String = ""
Dim byteArray As Byte()
Dim encoding As System.Text.Encoding
encoding = System.Text.Encoding.GetEncoding(1251)

If Not wordApp.CustomDictionaries.ActiveCustomDictionary Is Nothing Then
With wordApp.CustomDictionaries.ActiveCustomDictionary
filePath = .Path & "\" & .Name
End With
customDictionaryFile = System.IO.File.Open(filePath, IO.FileMode.OpenOrCreate)
customDictionaryFile.Position = customDictionaryFile.Length
byteArray = encoding.GetBytes(vbNewLine & wordToAdd)
customDictionaryFile.Write(byteArray, 0, byteArray.Length)
customDictionaryFile.Flush()
customDictionaryFile = Nothing
End If

Return True
Catch ex As Exception
Throw ex
End Try
End Function



Re: Visual Studio Tools for Office How to add words to MS Word custom dictionary

Cindy Meister

Opening the text file for writing would result in a possible conflict, so Word is probably unlinking from it. An alternative would be to try opening it inside the Word application.

Or, to relink it after writing, just set the ActiveCustomDictionary property, as the Help file shows

Set dicCustom = Application.CustomDictionaries _
.Add(FileName:="C:\Program Files" _
& "\Microsoft Office\Office\Home.dic")
Application.CustomDictionaries.ActiveCustomDictionary = dicCustom






Re: Visual Studio Tools for Office How to add words to MS Word custom dictionary

Stoil Pankov

Thanks for the answer, but there is a problem with that approach. After MS Word unlinks the dictionary it stays as a deleted object. When you add it again in MS Word there is no problem, but the next call to the ActiveCustomDictionary throws an exception ¡°Object has been deleted.¡± This could be easily shown by trying to add to words to the dictionary

Best regards

Stoil Pankov

Private Function AddToCustomDictionary(ByVal wordToAdd As String) As Boolean

Try

Dim customDictionaryFile As System.IO.FileStream

Dim customDictionary As Microsoft.Office.Interop.Word.Dictionary

Dim filePath As String = ""

Dim byteArray As Byte()

Dim encoding As System.Text.Encoding

encoding = System.Text.Encoding.GetEncoding(1251)

If Not wordApp.CustomDictionaries.ActiveCustomDictionary Is Nothing Then

With wordApp.CustomDictionaries.ActiveCustomDictionary

filePath = .Path & "\" & .Name ¡®Here an exception is thrown the second time the method is called

End With

customDictionaryFile = System.IO.File.Open(filePath, IO.FileMode.OpenOrCreate)

customDictionaryFile.Position = customDictionaryFile.Length

byteArray = encoding.GetBytes(vbNewLine & wordToAdd)

customDictionaryFile.Write(byteArray, 0, byteArray.Length)

customDictionaryFile.Flush()

customDictionaryFile = Nothing

customDictionary = wordApp.CustomDictionaries.Add(filePath)

wordApp.CustomDictionaries.ActiveCustomDictionary = customDictionary

End If

Return True

Catch ex As Exception

Throw ex

End Try

End Function





Re: Visual Studio Tools for Office How to add words to MS Word custom dictionary

Cindy Meister

Hi Stoil

<<Thanks for the answer, but there is a problem with that approach. After MS Word unlinks the dictionary it stays as a deleted object. When you add it again in MS Word there is no problem, but the next call to the ActiveCustomDictionary throws an exception ¡°Object has been deleted.¡± This could be easily shown by trying to add to words to the dictionary>>

Word won't be able to access the custom dictionary file as long as your application hasn't closed it. You set the object to Nothing, but that just orphans it in memory. You have to CLOSE it.






Re: Visual Studio Tools for Office How to add words to MS Word custom dictionary

Stoil Pankov

Thanks it is working perfectly now

Best regards

Stoil Pankov





Re: Visual Studio Tools for Office How to add words to MS Word custom dictionary

Jo.B

Hi,

Do you know a way to reload the dictionary after you add a word so that word will be recognize immediately with Word.

Because now, I add words but I must close and reopen Word for the new words in the dictionary to takes effects.

PS: I work with C#.

Thanks.





Re: Visual Studio Tools for Office How to add words to MS Word custom dictionary

Cindy Meister

Look at the Delete method of the CustomDictionary object as well as the Add method of the CustomDictionaries collection. The Delete method doesn't remove the file, just the link to the file. The Add method will re-instate the link.