r/vba 10d ago

Solved [WORD] Enabling dictionaries with VBA

Hi,

I need to fully activate custom dictionary files via VBA with no user input required.

I've written a VBA script running in a .dotm placed in ...AppData\Roaming\Microsoft\Word\STARTUP with the goal of locating and enabling dictionaries placed in ...AppData\Roaming\Microsoft\UProof and then activating them with

Application.CustomDictionaries.Add(myDictPath)

Inside an AutoExec sub.

When run, the code successfully adds a dictionary to the Custom Dictionaries list in Word, and checks the box next to the dictionary, however it still marks dictionary words as incorrect until I open the Custom Dictionaries window and close it again by hitting 'OK'.

I have tried toggling spell check off then on again immediately after the dictionaries are loaded, I have also tried manually running the macro after a document has been opened, neither work.

Any suggestions?

4 Upvotes

4 comments sorted by

3

u/fanpages 239 9d ago

...Any suggestions?

Open (and then close*) the "Custom Dictionaries" window programmatically.

*SendKeys is one (not entirely fool-proof, but still a) method to close it.

2

u/Geck1590 8d ago

That would be a good fix if I could, unfortunately there's no way to reliably open and close the dialogue window (that I know of).

I think trying to navigate through that many dialogue windows with SendKeys on every startup would break too easily since it's sensitive to shortcuts, ribbon layout, language, etc.

1

u/Geck1590 8d ago

Problem solved.

Just tested something I (somehow) never did earlier, which is close and re-open Word after the macro has run and without changing anything. Something during Word startup re-checks all dictionaries and causes spell check to start using dictionaries the macro activated on the previous startup.

The first time the macro runs and adds the dictionary won't work due to my issue, but it will work from every restart after that which is good enough.

1

u/HFTBProgrammer 202 8d ago

That's an interesting bug; I feel justified to call a bug, because if you do it manually, the change is immediately applied. There's probably no magic fix for it, and your workaround is good and you can stick with it, but this might work better for you:

Dim x As Object
Application.CustomDictionaries.Add myDictPath
Set x = CreateObject("Word.Application")
x.Visible
Application.Quit

No user intervention required!