April 9, 2024
Contribute to the Microsoft 365 and Office forum!
Click here to learn more 💡
May 10, 2024
Word Forum Top Contributors:
Auto-add to custom dictionaries on right-click?
Report abuse
Thank you.
Reported content has been submitted
There are three parts to the solution. The first part is to put into place three macros involved in actually adding the selected word to one of the two dictionaries. The second part is to put in a macro that will add the desired entries to the right-click menu (because there is no other way to modify that menu in Word 2007 or 2010). The third part is to use the Options > Proofing > Custom Dictionaries dialog to create the two dictionaries and add them to the dictionaries collection, if they aren't already there.
The first part:
Use the instructions in the "Modules" section of the installing_macro.htm page to create a module in your Normal template and name it ContextMacros. (This will be important in the second part.) Then copy the following code and paste it into the ContextMacros module.
Sub AddToMyPatientNames()
Dim dicFile As String
dicFile = "MyPatientNames.dic"
AddToCustomDictionary dicFile
End Sub
Sub AddToMyMTProducts()
Dim dicFile As String
dicFile = "MyMTProducts.dic"
AddToCustomDictionary dicFile
End Sub
(edit) IMPORTANT: This part of the code, up to the "STOP HERE", is incorrect. Replace it with the code from my later reply.
Private Sub AddToCustomDictionary(dicFile As String)
Dim thisDict As Dictionary
Dim testStr As String
Dim newWord As String
Dim rg As Range
On Error GoTo errhdl
newWord = Selection.Words(1).SpellingErrors(1).Text
'Get the path to the custom dictionary
Set thisDict = CustomDictionaries(dicFile)
dicFile = thisDict.Path & "\" & dicFile
'Check for existence of file
testStr = Dir(dicFile)
If Len(testStr) = 0 Then
Open dicFile For Output As #1
Else
Open dicFile For Append As #1
End If
Print #1, newWord
Close #1
'Turn off spelling wavy underlines for this word
Set rg = ActiveDocument.Range
With rg.Find
While .Execute(FindText:=newWord, Wrap:=wdFindStop)
rg.SpellingChecked = True
Wend
End With
Exit Sub
errhdl:
If Err.Number = 5941 Then
MsgBox dicFile & " not found in Dictionaries collection"
Else
MsgBox "Error " & Err.Number & vbCr & Err.Description
End If
End Sub
(edit) STOP HERE
Save the Normal template by using the Save command in the macro editor (not in the main Word window).
The second part:
Copy/paste the following code into the ContextMacros module (or any other module; this isn't critical).
Sub BuildControls()
Dim cb As CommandBar
Dim octrl As CommandBarControl
Dim oBtn As CommandBarButton
Dim bPatient As Boolean, bProduct As Boolean
'On Error GoTo errhdl
'Make changes to the Normal template
CustomizationContext = NormalTemplate
'Add new entries on the Text context menu
Set cb = CommandBars("Spelling")
'Avoid duplication
For Each octrl In cb.Controls
If (octrl.Caption = "Add to MyPatientNames") Then
MsgBox "PatientNames entry exists already"
bPatient = True
End If
If (octrl.Caption = "Add to MyMTProducts") Then
MsgBox "Products entry exists already"
bProduct = True
End If
Next octrl
If bPatient Then GoTo skipPatient
'Add entry
Set oBtn = cb.Controls.Add(Type:=msoControlButton, Before:=1)
With oBtn
.Caption = "Add to MyPatientNames"
.Style = msoButtonCaption
'Identify the module and procedure to run
.OnAction = "ContextMacros.AddToMyPatientNames"
End With
Set oBtn = Nothing
MsgBox "Added PatientNames entry"
skipPatient:
If bProduct Then GoTo done
'Add entry
Set oBtn = cb.Controls.Add(msoControlButton, Before:=2)
With oBtn
.Caption = "Add to MyMTProducts"
.Style = msoButtonCaption
'Identify the module and procedure to run
.OnAction = "ContextMacros.AddToMyMTProducts"
End With
Set oBtn = Nothing
MsgBox "Added Products entry"
done:
Set cb = Nothing
Exit Sub
errhdl:
MsgBox Err.Description
End Sub
Sub RemoveControls()
Dim oCtr As CommandBarControl
CustomizationContext = NormalTemplate
For Each oCtr In Application.CommandBars("Spelling").Controls
If (oCtr.Caption = "Add to MyPatientNames") Or _
(oCtr.Caption = "Add to MyMTProducts") Then
oCtr.Delete
End If
Next oCtr
End Sub
Now, this needs to be done only once: Put the cursor on the Sub BuildControls() line and press F5 to run the macro. That will create the two entries at the top of the right-click menu that pops up when you right-click a word that's marked as a spelling error. (It won't appear on any other right-click menu.)
The RemoveControls() macro is there just in case you ever want to remove the two entries from the Spelling menu.
The third part:
If the MyPatientNames.dic and MyMTProducts.dic custom dictionaries don't appear on the Options > Proofing > Custom Dictionaries dialog or aren't checked to make them available, then the macros will pop up an error message when you try to use them. Go to the dialog and make sure the custom dictionaries are available.
https://jay-freedman.info
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
I don't believe there's any difference between Word 2007 and Word 2010 in this area. What I think happened is that you may have missed this sentence in my initial post:
"The third part is to use the Options > Proofing > Custom Dictionaries dialog to create the two dictionaries and add them to the dictionaries collection, if they aren't already there."
Your my3() macro does the same thing, adding the dictionaries to the CustomDictionaries collection.
https://jay-freedman.info
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Question Info
Last updated March 29, 2024 Views 783 Applies to: