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.
- Create a user-defined dictionary
Custom1.
Menu / Tools / Options… / Languages And Locales / Writing Aids.
User-defined Dictionaries: New Name:Custom1OK OK - Open the attached file (DocDict.odt) and turn on Spelling (F7). The word
NewWordshould have a wavy underline. Add this field to the dictionary (Custom1.dic). The underline should disappear. - Create a new text file and enter the word
NewWordfollowed by a space. The wordNewWordshould be underlined. - Return to the DocDict.odt file. The word
NewWordis not underlined.
How it’s done.
- The
CustomDictNametext property with the valueCustom1.dichas been added to theDocDict.odtcustom properties. - Handlers have been assigned to the
DocDict.odtdocument 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)