HowTo: Use the Microsoft Word Object Model capabilities for spell checking
The Microsoft Word Object Model contains a lot of capabilities that .NET developers can benefit from. While everybody knows about the abilities of Microsoft Word related to text formatting, few are aware of its rich capacity in the spell check area.
Let’s try to make use of this capacity now. We will need a Microsoft Word COM addin in which we use an ordinary .net form with the RichTextBox control and two Buttons. You type some text into the RichTextBox, later on this text will be spell checked on a button click. The code of button click event handler is shown below:
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles btnRun.Click Dim addinModuleObj As AddinModule = TryCast _ (AddinModule.CurrentInstance, AddinModule) richTextBox.SelectAll() richTextBox.SelectionColor = Color.Black Dim Template As Object = Type.Missing Dim NewTemplate As Object = Type.Missing Dim DocumentType As Object = Type.Missing Dim Visible As Object = False Dim docs As Word.Documents = addinModuleObj.WordApp.Documents If docs IsNot Nothing Then Try Dim document As Word._Document = docs.Add(Template, _ NewTemplate, DocumentType, Visible) If document IsNot Nothing Then Try Dim range As Word.Range = document.Content If range IsNot Nothing Then Try range.Text = richTextBox.Text Dim Count As Integer = _ FindSpellingErrors(document) Dim SaveChanges As Object = _ Word.WdSaveOptions.wdDoNotSaveChanges Dim OriginalFormat As Object = Type.Missing Dim RouteDocument As Object = Type.Missing document.Close(SaveChanges, OriginalFormat, _ RouteDocument) If Count = 0 Then MessageBox.Show("There are no errors!", _ "Add-in Express HowTo") Else MessageBox.Show(String.Format( _ "{0} Error(s) were found!", Count), _ "Add-in Express HowTo") End If Finally Marshal.ReleaseComObject(range) End Try End If Finally Marshal.ReleaseComObject(document) End Try End If Finally Marshal.ReleaseComObject(docs) End Try End If End Sub
And here is the FindSpellingErrors method used in the code above:
Private Function FindSpellingErrors(ByVal document As Word._Document) _ As Integer Dim ret As Integer = 0 document.SpellingChecked = False Dim spellingErrors As Word.ProofreadingErrors = TryCast( _ document.SpellingErrors, Word.ProofreadingErrors) If spellingErrors IsNot Nothing Then Try ret = spellingErrors.Count For i As Integer = 1 To spellingErrors.Count Dim itemError As Word.Range = spellingErrors.Item(i) If itemError IsNot Nothing Then Try richTextBox.Find(itemError.Text, _ itemError.Start, _ RichTextBoxFinds.MatchCase Or _ RichTextBoxFinds.WholeWord) richTextBox.SelectionColor = Color.Red Finally Marshal.ReleaseComObject(itemError) End Try End If Next Finally Marshal.ReleaseComObject(spellingErrors) End Try End If Return (ret) End Function
There is one unpleasant thing about the ProofreadingErrors collection – it is slow. I don't know the exact reason why it is so, I just feel a caveat should be made about this.
Also note that you can use the capabilities of the Word Object Model not only from inside of a COM add-in, but in a standalone application as well.
You may also be interested in:
Creating Word COM add-in step-by-step
Available downloads:
The sample add-ins below were written using Add-in Express 2008 for Office and .net
C# sample Word add-in for VS 2005
VB.NET sample Word add-in for VS 2005
13 Comments
Hi,
Which reference do we add to our project to use the AddinModule?
Hi Danish,
You don’t need to add a reference to Add-in Express assembly to your existing project. The AddinModule in this sample is used to access the Word.Application object only. So, you can modify the sample code above and avoid using AddinModule.
am using visual basic 2008 not visual basic.net am new to vb, my question is how can i add spelling suggestion to my application with ms word object. thanks in advance.
Hi Dominic,
You can try to use the GetSpellingSuggestions method of Word’s Application object:
https://msdn.microsoft.com/en-us/library/office/ff835170%28v=office.14%29.aspx
Also, please have a look at the following articles, hopefully you will find them useful:
https://www.vbforums.com/showthread.php?259140-GetSpellingSuggestions-in-Word-COM
https://www.informit.com/articles/article.aspx?p=27219&seqNum=11
Thanks for your reply but it didn’t solve my problem, i we prefer usine your code above if you can tell me the references to use to make (dim) work in my visual studio 2008 pls
Hi Dominic,
You need to add the Microsoft.Office.Interop.Outlook assembly.
this solve my problem of spelling sugestion code= imports microsoft.office.interop.word public wd as application public function chkspelin() wd=creatobject(“word.Application”) dim spellsug as word.spellingsuggestions dim i% dim strbuffer as string strbuffer=txtbox1.text word.document.add() wd.visible=false spellsug=wd.getspellingsuggestions(strbuffer) for i%=1 to spellsug.count list1.items.add(spellsug(i%.name)) next i% wd.quit(false) wd=nothing end function
pls help having problem searching for word in a textbox, for example textbox1.text=”DOM a b c d e f g k JOHN l m w t p m a j PHILIP” My question is how do i search and select only the text between DOM and JOHN using vb.net
Hi Dominic,
Please have a look at the following MSDN articles, hopefully they will help you:
How to: Search Within a String (Visual Basic)
TextBoxBase.SelectionStart Property
TextBoxBase.SelectionLength Property
I need help on how to get the usercode, model, battery voltage of my mobile phone connected to my PC using VB.net
any help will be appreciated thanks
Hi Dominic,
Sorry, I cannot suggest anything of value because this is not our area of specialization.
Is it possible to do the same kind of spellchecking in Outlook and if yes then how?
Hello Leon,
The only difference is the way you obtain the document to spell-check. In Outlook, you get it via the Inspector.WordEditor property that returns a Word.Document.