private void ThisAddIn_Startup(object sender, System.EventArgs e) { // Globals.ThisAddIn.Application.Cells.Worksheet.Change += new Excel.DocEvents_ChangeEventHandler(Worksheet_Change); } void Worksheet_Change(Excel.Range Target) { SpellCheck.SpellChecker(Target); }
private void OrfoCheckAll_Click(object sender, RibbonControlEventArgs e) { var excel = Globals.ThisAddIn.Application; var wss = excel.Worksheets; var app = excel.Application; foreach (var ws in wss) { var sheet = ws as Excel.Worksheet; if (sheet != null) { var range = sheet.UsedRange; foreach (var cll in range) { var cell = cll as Excel.Range; SpellCheck.SpellChecker(cell); } } } }
private void OrfoCheckCurrentSheet_Click(object sender, RibbonControlEventArgs e) { var excel = Globals.ThisAddIn.Application; var app = excel.Application; var sheet = app.ActiveSheet as Excel.Worksheet; if (sheet != null) { var range = sheet.UsedRange; foreach (var cll in range) { var cell = cll as Excel.Range; SpellCheck.SpellChecker(cell); } } }
public static class SpellCheck { public static void SpellChecker(Excel.Range Target) { var app = Globals.ThisAddIn.Application.Application; string str = Target.Text.ToString(); if (app.CheckSpelling(str, Type.Missing, true) == false) { foreach (string tmp in ((string)str).Split(' ')) { if (app.CheckSpelling(tmp, Type.Missing, Type.Missing) == false) { if (Target.Comment == null) { Target.AddComment(" " + tmp); Target.Characters[str.IndexOf(tmp) + 1, tmp.Length].Font.ColorIndex = 3; } else { Excel.Characters c = Target.Comment.Shape.TextFrame.Characters(Type.Missing, Type.Missing); if (!c.Caption.Contains(tmp)) { c.Caption = c.Caption + " " + tmp; } Target.Characters[str.IndexOf(tmp) + 1, tmp.Length].Font.ColorIndex = 3; } } else { Target.Characters[str.IndexOf(tmp) + 1, tmp.Length].Font.ColorIndex = 0; } } } else { if (Target.Comment != null) { if (Target.Comment.Shape.AlternativeText.Contains(" ")) { Target.Characters[str.IndexOf(str), str.Length].Font.ColorIndex = 0; Target.Comment.Delete(); } } } }
Source: https://habr.com/ru/post/136520/
All Articles