Reputation: 45
Morning Everyone, Been playing with this for a couple of days and getting no where. I am creating a custom richtextbox in vb.net that will underline spelling errors and offer suggestions when the misspelled word is right clicked on (I can't believe this isn't default in textboxes/richtextboxes...anyway). I have the underlining working well but I keep getting the error: This command is not available because no document is open.
EDIT
running on desktop, 64 bit, office 2007 is installed. Here is the entire class and how I am testing the call. IsWordWrong works great. SpellingSuggestions fails on wapp.GetSpellingSuggestions(pWord) with the error "This command is not available because no document is open" which according to MSDN and multiple tutorials I have seen, should not happen:
Public Class SpellCheckUtility
Private Shared wapp As Word.Application
Private Shared missing As Object = Reflection.Missing.Value
Public Shared Sub StartApp()
If IsNothing(wapp) Then
wapp = New Word.Application
wapp.Visible = False
wapp.WindowState = 0
End If
End Sub
Public Shared Function IsWrongWord(ByVal pWord As String) As Boolean
StartApp()
Dim oFalse As Object = False
Dim activedoc As Word.Document = wapp.Documents.Add(, , , oFalse)
Dim m_range As Word.Range
m_range = activedoc.Range
m_range.InsertAfter(pWord)
Dim SpellErrors As Word.ProofreadingErrors = m_range.SpellingErrors
Return SpellErrors.Count > 0
End Function
Public Shared Function SpellingSuggestions(ByVal pWord As String) As Generic.List(Of String)
Dim rtnlist As New Generic.List(Of String)
If pWord.Length > 0 Then
StartApp()
Dim SpellErrors As Word.SpellingSuggestions = wapp.GetSpellingSuggestions(pWord)
For m_word As Integer = 1 To SpellErrors.Count
rtnlist.Add(SpellErrors.Item(m_word).Name)
Next
End If
Return rtnlist
End Function
Public Shared Sub dispose()
If Not (wapp Is Nothing) Then
Dim m_saveChanges As Object = False
wapp.Quit(m_saveChanges)
wapp = Nothing
End If
End Sub
End Class
How called:
Private Sub btnclick1_Click(sender As Object, e As EventArgs) Handles btnclick1.Click
Dim wordlist As Generic.List(Of String) = SpellCheckUtility.SpellingSuggestions("thingz")
End Sub
I have tried both the wapp.GetSpellingSuggestions and the m_range.GetSpellingSuggestions both with the same results. I am using m_range.SpellingErrors somewhere else and that works just fine and the setup to get the range is exactly the same so not sure what I am doing wrong.
Any help is GREATLY appreciated!!
**adapting this code to what I actually want http://www.codeproject.com/Articles/18799/Spell-check-and-underline-the-wrong-word-using-Mic
Upvotes: 0
Views: 297
Reputation: 129
I was able to resolve a similar issue with these symptoms by using
Dim activedoc = wapp.Documents.Add()
rather than
Dim activedoc = wapp.Documents.Add(, , , False)
Upvotes: 0
Reputation: 45
whooohoo for 64bit vs 32bit office install quirks! So my dev machine is 64 but my office install is 32. Apparently interop just doesn't like that setup and (here on is guessing) it is missing a folder to put the temp open doc when using wapp.Documents.Add() hence the no open doc error. Instead of creating the folder itself, like it should...and really spelling checks should be a default yes/no flag in the properties of all text input controls by now...seriously, it just fails.
Rant over, here is the line I added to the top of the StartApp() function and everything works fine now.
If Not IO.Directory.Exists("C:\Windows\SysWOW64\config\systemprofile\") Then IO.Directory.CreateDirectory("C:\Windows\SysWOW64\config\systemprofile\")
Upvotes: 0