InterOp COM error - C# 2.0 - Office 2003
-
Monday, October 17, 2005 1:17 PMUsing VS 2005 RC1 and Office 2003 11.6568.6568 SP2.
In the following short class, when I call the GetSpellingSuggestions I get this error:
"Hresult: 0x80010105 RPC_E_SERVERFAULT"
I'm running as User (as opposed to Admin/Power User). if I Run As as an Admin I get the same error. Im using Windows XP SP2.
public
class CheckSpelling{
private static CheckSpelling spellChecker; private Microsoft.Office.Interop.Word.Application wordApp; // private Microsoft.Office.Interop.Word.ApplicationClass wordApp; /// <summary> /// Singleton instance /// </summary> public static CheckSpelling SpellChecker{
get { if (CheckSpelling.spellChecker == null){
CheckSpelling.spellChecker = new CheckSpelling();}
return CheckSpelling.spellChecker;}
}
/// <summary> /// For static which need initializing /// </summary> static CheckSpelling(){
}
/// <summary> /// Private for singleton /// </summary> private CheckSpelling(){
wordApp =
new ApplicationClass();}
/// <summary> /// Checks a single word for misspelling. The return value indicates true for misspelling or false for correct spelling. The output parameter will have the suggestions if misspelled. /// </summary> /// <param name="word">The word that needs checking. Any whitespace will cause only the preceding text to be searched</param> /// <param name="suggestions">If the word is misspelled , this array will have the suggusted fixes.</param> /// <returns>ture if misspelled, false if correctly spelled</returns> public bool CheckWordIsMisspelled(string word, out string[] suggestions){
try{
object nada = Type.Missing; object falseObject = false; object wordType = WdSpellingWordType.wdSpellword; SpellingSuggestions spellingSuggestions = wordApp.GetSpellingSuggestions(word, ref nada, ref falseObject, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada); if (spellingSuggestions.Count > 0){
string[] results = new string[spellingSuggestions.Count]; for (int i = 0; i < spellingSuggestions.Count; i++){
results
= spellingSuggestions
.ToString();}
suggestions = results;
return true;}
else{
suggestions =
null; return false;}
}
catch(System.Runtime.InteropServices.COMException com){
throw com;}
catch(Exception ex){
throw ex;}
}
}
All Replies
-
Thursday, October 27, 2005 4:31 AM
Word's GetSpellingSuggestions method won't work unless you have at least one open document. One simple solution is to create a new document in the constructor of your CheckSpelling class, as shown below.
Also, the code in your CheckWordIsMisspelled method is incorrect: the SpellingSuggestions collection is 1-based, and it's not clear from your post what you're assigning each element to (lightbulbs in your post). Corrected code below.
private CheckSpelling(){
wordApp = new Microsoft.Office.Interop.Word.Application();
// Call this in order for GetSpellingSuggestions to work later
object missing = Type.Missing;
wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
}
public bool CheckWordIsMisspelled(string word, out string[] suggestions)
{
suggestions = null;
bool retval = false;
try
{
object nada = Type.Missing;
object falseObject = false;
object wordType = WdSpellingWordType.wdSpellword;
SpellingSuggestions spellingSuggestions = wordApp.GetSpellingSuggestions(
word, ref nada, ref falseObject, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada, ref nada);
if (spellingSuggestions.Count > 0)
{
string[] results = new string[spellingSuggestions.Count];
for (int i = 0; i < spellingSuggestions.Count; i++)
{
results
= spellingSuggestions[i+1].Name;}
suggestions = results;
return retval;
}
}
catch (Exception)
{
throw;
}
return retval;
}

