|

Introduction
This little function written in C# will invoke Microsoft Word's spellchecker and check the spelling of a given textbox.
Background
I had written a program in C# in which a spellchecker could be useful. Since I had a hard time finding any information on the net or in the MSDN Library for .NET, on how to use Word's spellchecker from a C# application I was wondering if it was because it was a trivial affair to program, something only a few people would use or because it was actual difficult to program. I decided to dig deeper into it. A VBA script that I found on the net convinced me that it would be possible to find a solution. The following article in MSDN "AutoWord Sample: Demonstrates Automating Microsoft Word" pointed in the right direction and from there it was straight forward.
Using the code
Remember to reference the Microsoft Word xx Object Library. (Interop.Word.dll) There are so many properties and functions for the document object that if you want to use a similar logic for a RichTextBox it should be fairly simple. ...
using Word;
using System.Reflection;
........................
private void button1_Click(object sender, System.EventArgs e)
{
fSpellCheck(textBox1 , label1 );
}
public void fSpellCheck(TextBox tBox, Label lLbl)
{
int iErrorCount = 0;
Word.Application app = new Word.Application();
if (tBox.Text.Length > 0)
{
app.Visible=false;
object template=Missing.Value;
object newTemplate=Missing.Value;
object documentType=Missing.Value;
object visible=true;
object optional = Missing.Value;
_Document doc = app.Documents.Add(ref template,
ref newTemplate, ref documentType, ref visible);
doc.Words.First.InsertBefore (tBox.Text );
Word.ProofreadingErrors we = doc.SpellingErrors;
iErrorCount = we.Count;
doc.CheckSpelling( ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional,
ref optional, ref optional);
if (iErrorCount == 0)
lLbl.Text = "Spelling OK. No errors corrected ";
else if (iErrorCount == 1)
lLbl.Text = "Spelling OK. 1 error corrected ";
else
lLbl.Text = "Spelling OK. " + iErrorCount +
" errors corrected ";
object first=0;
object last=doc.Characters.Count -1;
tBox.Text = doc.Range(ref first, ref last).Text;
}
else
lLbl.Text = "Textbox is empty";
object saveChanges = false;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
| You must Sign In to use this message board. |
|
| | Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh) | FirstPrevNext |
|
|
 |
|
|
Problem is is that checkspelling is working, but because word isn't visible, it's running "in the background". If you set your word app to visible, I think you'll see the dialog box. This is happening in some machines only. Below is the code. Please help....
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); if (str.Length > 0) { app.Visible = false; object template = Missing.Value; object newTemplate = Missing.Value; object documentType = Missing.Value; object visible = true; object optional = Missing.Value; object saveChanges = false; object originalFormat = Missing.Value; object routeDocument = Missing.Value;
Microsoft.Office.Interop.Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); doc.Words.First.InsertBefore(str);
app.WindowState = 0; app.Top = -3000; doc.Application.Visible = true; Clipboard.SetDataObject(str);
doc.Activate();
doc.CheckGrammar(); object beg = 0; object end = 0; doc.Range(ref beg, ref end);
object first = 0; object last = doc.Characters.Count - 1;
str = doc.Range(ref first, ref last).Text;
doc.Close(ref saveChanges, ref originalFormat, ref routeDocument); app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, Have you noticed about this problem: Once we launch the spell checker, and click any other application e before the spell checker is complete/close, it will cause this spell checker application to hang. I think this bug is from Microsoft. Do you know how we can avoid it? 
Rgds, Kiky
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Same thing, it happens from time to time, this is part of my code
Dim word_server As New Microsoft.Office.Interop.Word.Application ' Hide the server. word_server.Visible = False
' Make a Word Document. Dim doc As New Microsoft.Office.Interop.Word.Document doc = word_server.Documents.Add() Dim iErrorCount As Integer = 0
' Copy the text into the Document. Clipboard.Clear() Clipboard.SetText(txtISSUE.Text) doc.Content.Paste() Dim we As Microsoft.Office.Interop.Word.ProofreadingErrors = doc.SpellingErrors iErrorCount += we.Count doc.CheckGrammar() doc.Content.Select() doc.Content.Copy() ' all this to avoid the squares on text
txtISSUE.Text = Clipboard.GetText(TextDataFormat.Text) MessageBox.Show("Spell Check and Grammar Check Finished Errors Found:" + iErrorCount.ToString(), "SpellCheck") doc.Close(SaveChanges:=False)
btw I know this is vb but is kind the same.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm trying to create spell-checking service (raw HTTP). The checking is done in IHttpHandler. But no matter what text I try to check, I always get 0 errors. The same code that I use in the handler works ok (ie. it finds errors) in a standalone application.
Anyone has a suggestion? Thank you!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
If you want more then just the count of errors, try this code:
Word.ProofreadingErrors proofErrors = range.SpellingErrors; for (int i = 1; i <= proofErrors.Count; i++) { lstErrors.Items.Add(proofErrors[i].Text); }
And for a list of suggestions of corrections to the errors:
object oMissing = System.Reflection.Missing.Value; Word.SpellingSuggestions suggestions = m_WordApplication.GetSpellingSuggestions (lstErrors.Text, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
for (int i = 1; i <= suggestions.Count; i++) { lstSuggestions.Items.Add(suggestions[i].Name); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, I want to use the spell checker in a website (usign ASP.NET in C#) but without seeing it's dialog. In other meaning,I want if a word (I have checked it)is true it return true,if it returns it's suggestion words. I'm sorry for annoyance. Thanks for advice.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
Well, I see this approach is very useful, but after the spellchecking dialog closed, the screen splashed because the document is closed. How can I prevent this? How can I close the document silently without splashing?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Hi!! I prevent this by changing this line:
"object visible=true;"
for
"object visible=false;"
So, the Word Application don´t appears anymore. PS: I sorry by my English, because I´m Brazilian.
Gustavo Barros
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Friend,
I had change the code from "object visible = true;" TO "object visible = false;" then i got exception "The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))" if this line of code is true then now exception comes. Please guide me and also i want to show this spellcheker as showdialog.
Regards ,
Hawk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, This solution is for a desktop application... How can I use Microsoft word's dlls to use for spell check in a web application? Is it possible? Thanks, sb
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Put this in your asp page:
<SCRIPT LANGUAGE=vbscript> <!-- 'SpellChecker ' PURPOSE: This function accepts Text data for which spell checking has to be done. ' Return's Spelling corrected data ' Function SpellChecker(TextValue)
Dim objWordobject Dim objDocobject Dim strReturnValue Dim strVersionNum, strBuild, bReturn
'Create a new instance of word Application
Set objWordobject = CreateObject("word.Application") objWordobject.WindowState = 2 objWordobject.Visible = False strVersionNum = objWordobject.Version strBuild = objWordobject.Build
'Create a new instance of Document
Set objDocobject = objWordobject.Documents.Add( , , 1, True) objDocobject.Content=TextValue bReturn= objDocobject.CheckSpelling
'Return spell check completed text data strReturnValue = objDocobject.Content
'Close Word Document objDocobject.Close false
'Set Document to nothing Set objDocobject = Nothing
'Quit Word objWordobject.Application.Quit True
'Set word object to nothing Set objWordobject= Nothing
MsgBox "The spelling check is complete",vbOKOnly, "Spell Check"
SpellChecker=strReturnValue' & "Build:" & strBuild
End Function
-->
</SCRIPT>
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Nyland, Thanks for the codes. I tried this in C#. I have a reference set to interop.word.dll and at the point where I create a word application as
Word.Application app = new Word.Application();
it throws an authentication error: Access is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Could you please give any suggestions? WOuld it be possible to use Microsoft word.dll for spell check in a web application wihtout creating a security hole?
Thanks again, Saritha
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You should impersonate. A user who has rights to run office applications will be fine.
When you impersonate, it works but somehow you cant see the spell check dialog.
For more than a thousand years her sweet madness Has murmured its ballad to the evening breeze.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
FYI for everyone that is thinking about using this approach:
The EULA specifically disallows 3rd parties from binding to objects like the word spell checker outside of MS word.
If anybody interested in much better approach, then get it from the following.
Use Word's spell checker from .net[^]
Demo application works under .NET 1.1 version. But on the top of the article it mentioned .NET 1.0
"If a jug falls upon a stone, woe to the jug. If a stone falls upon a jug, woe to the jug. Always woe to the jug"." - KaЯl This signature was created by "Code Project Quoter".
|
| Sign In·View Thread·PermaLink | 3.88/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
Here's an archived copy: http://web.archive.org/web/20030603165215/http://www.dotnetdog.com/archives/000002.html
And here's what Ray Osherove thought of it:
"Looking at the wrapper code, you gotta wonder why doing LateBound COM manipulation is so darn clumsy and wordy. it looks like it could be much more elegant...."
Commentary from: http://weblogs.asp.net/rosherove/archive/2003/02/19/2659.aspx
Unfortunately, I can't find MSWordWrapper.zip and http://www.dotnetdog.com/archives/MSWordWrapper.zip doesn't work. :/
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
I just reviewed the Word 2003 EULA and it has no mention of this or anything even remotely close to this.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|