Is there a way to create a document specific custom dictionary

I want a custom dictionary for one specific document without having to turn it on and off in tools-opptions when switching between documents. Is there a way to do this?

I don’t know the standard way.
It can be implemented using macros.
The name of the document-specific dictionary can be specified in the file’s custom properties.

The attached file demonstrates this technique.

  1. Create a user-defined dictionary Custom1.
    Menu / Tools / Options… / Languages ​​And Locales / Writing Aids.
    User-defined Dictionaries: New Name: Custom1 OK OK
  2. Open the attached file (DocDict.odt) and turn on Spelling (F7). The word NewWord should have a wavy underline. Add this field to the dictionary (Custom1.dic). The underline should disappear.
  3. Create a new text file and enter the word NewWord followed by a space. The word NewWord should be underlined.
  4. Return to the DocDict.odt file. The word NewWord is not underlined.

How it’s done.

  1. The CustomDictName text property with the value Custom1.dic has been added to the DocDict.odt custom properties.
  2. Handlers have been assigned to the DocDict.odt document activation and deactivation events. These handlers activate the custom dictionary when the document is activated, and deactivate it when the document is deactivated.
Option Explicit

Const DictName As String = "CustomDictName"

Sub OnFocusGained(oEvent As Object)
    Dim sDictName As String
    sDictName = GetCurrentDocDictName(oEvent.Source)
    SetDictionaryActiveState(sDictName, True)
End Sub

Sub OnFocusLost(oEvent As Object)
    Dim sDictName As String
    sDictName = GetCurrentDocDictName(oEvent.Source)
    SetDictionaryActiveState(sDictName, False)
End Sub

Sub SetDictionaryActiveState(ByVal sDictName As String, Byval bState As Boolean)
    Dim oDicList As Object
    Dim oDic As Object
    On Error GoTo ErrorHandler:
    oDicList = CreateUnoService("com.sun.star.linguistic2.DictionaryList")
    oDicList.getDictionaryByName(sDictName).setActive(bState)
    Exit Sub
     
ErrorHandler:    
    Msgbox "Error: SetDictionaryActiveState " & sDictName & " " & bState
End Sub

' Extracts the target dictionary name from User Defined Document Properties
Function GetCurrentDocDictName(Byval oDoc as Object) As String
    GetCurrentDocDictName = ""
    On Local Error GoTo ErrorHandler
    GetCurrentDocDictName = oDoc.DocumentProperties.UserDefinedProperties.getPropertyValue(DictName) 
    
ErrorHandler:
End Function

DocDict.odt (10.4 KB)