Spell Checker using word.
I came across this code on the net and was wondering if anyone could help me tweak this to search more than just one textbox.
Thanks
protected void Button1_Click(object sender, EventArgs e)
{
Word.Application app = new Word.Application();
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;
Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
doc1.Words.First.InsertBefore(TextBox1.Text);
doc1.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);
object first = 0;
object last = doc1.Characters.Count - 1;
TextBox1.Text = doc1.Range(ref first, ref last).Text;
app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
Antworten
I would suspect something like this would work for you. assuming this function is part of the form that contains the text boxes you want to check.
foreach (Object o in Controls)
{
if (!(o is TextBox))
continue;
TextBox tb = (o as TextBox);
doc1.Words.First.InsertBefore(tb.Text);
doc1.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);
object first = 0;
object last = doc1.Characters.Count - 1;
tb.Text = doc1.Range(ref first, ref last).Text;
}
Alle Antworten
do you mean from this line?
doc1.Words.First.InsertBefore(TextBox1.Text);
are you able to explain a tad more in what you like to achieve so we can better assist you?
I've got a form that I'd like to use MS Word's spell check capability to check the entire form not just one textbox, like the example does. I was wondering how to go about adding more textboxes to this code for it to check instead of just checking textbox1.
Thanks,
I would suspect something like this would work for you. assuming this function is part of the form that contains the text boxes you want to check.
foreach (Object o in Controls)
{
if (!(o is TextBox))
continue;
TextBox tb = (o as TextBox);
doc1.Words.First.InsertBefore(tb.Text);
doc1.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);
object first = 0;
object last = doc1.Characters.Count - 1;
tb.Text = doc1.Range(ref first, ref last).Text;
}if I understand correctly, you wish to have say, several textboxes on your form, and you wish to do some word searching in MS Word, using the values from these several textboxes on your application form correct? please do correct me if I am wrong.
If this is the case, you can go through each textbox on the form, and then use your code to get the value of the textbox, in the for each loop, and then do the search based on that. Example (I'm unsure if it should be from this line but hopefully itll give you an insight):
foreach(Control currentControl in this.Controls)
{
if (currentControl.GetType() == typeof(TextBox))
{
TextBox theTextBox = (TextBox)currentControl;
//rest of code here...
doc1.Words.First.InsertBefore(theTextBox.Text);
//and other code here
}
}
Does this help/make sense?
I've got a form that I'd like to use MS Word's spell check capability to check the entire form not just one textbox, like the example does. I was wondering how to go about adding more textboxes to this code for it to check instead of just checking textbox1.
Thanks,
- the code I supplied does just that, of course you would need to add the textboxes to the form, then code then goes through each textbox on the form and does the search I believe
- Would this work for a web form as well?
- If this isn't the right forum to post this addon question then please let me know where to post it. Thanks!
ASP.NET questions should be posted:
http://forums.asp.net for a better answer and since the site is dedicated for ASP.NET, you'll be sure to get a good answer since thats where the ASP.NET experts hang out

