public WordCollection(Word.Words sentence, ErrorList errors, int sentenceNumber) { this.sentence = sentence; this.errorList = errors; this.sentenceNumber = sentenceNumber; this.wordUnderline = new Word.WdUnderline[sentence.Count]; this.wordColor = new Word.WdColor[sentence.Count]; markWordsIfError(); }
private void openDocument(String file_path) { Word.Application app = new Word.Application(); Word.Document doc = app.Documents.Open(file_path); app.Visible = false; doc.Activate(); Word.Words wds = doc.Sections[1].Range.Words; //Iterate the word need to change font foreach (Word.Range wd in wds) { //if (wd.Text.Equals("<") || wd.Text.Equals(">") || wd.Text.Equals("name") || wd.Text.Equals("address") || wd.Text.Equals("city") || wd.Text.Equals("state") || wd.Text.Equals("zip") || wd.Text.Equals("company")) //{ // wd.Font.Color = Word.WdColor.wdColorBlue; //} ////Try if it works if (wd.Text.Equals(" ")) { continue; //Do nothing } else if (wd.Font.Name.Equals("Preeti")) { wd.Text.Replace(wd.Text, "NULL"); } //Try finish } FindAndReplace(app, "<name>", "Ram prasad"); FindAndReplace(app, "<address>", "00 Main street"); doc.Save(); doc.Close(); app.Quit(); MessageBox.Show("Task Completed."); }
private void ApplicationOnWindowBeforeDoubleClick(Word.Selection selection, ref bool cancel) { // Get the selected word Word.Words words = selection.Words; MessageBox.Show("Selection: " + words.First.Text); }
private void CheckNext_U() { nStartPos += nWordLen; if (bStart) { // extract all possible punctuation characters // HM: 2009-11-27 removed the dash char as word separator and used it for hyphen, // the em-dash is assumed to be used for word separator string[] charSeparators = new string[] { "\n", "\r", "\t", "\f", "\v", " ", ",", ".", ";", ":", "\"", "'", "?", "&", /*"-",*/ "–", "_", "|", "/", "<", ">", "[", "]", "{", "}", "(", ")", "«", /*"»",*/ "…", "‘", "’", "‚", "“", "”", "„", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "®", "¬", "\u055D", "\u0589" }; string sText = sParText.Substring(nParOffset); ArWords = sText.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); nStartPos = nParOffset; nParOffset = 0; j = 0; btnAdd.Enabled = btnChange.Enabled = false; } if (j < ArWords.Length && !bRestartPar) { lstWords.Items.Clear(); sSCIIOutput = ""; string sInput = ArWords[j].ToString(); string sInputStartDel = ""; string sInputEndDel = ""; int nNHStartPos = nStartPos; nStartPos = sParText.IndexOf(sInput, nStartPos, StringComparison.Ordinal); nWordLen = sInput.Length; int nNHLen = nWordLen; if (nNHStartPos < nStartPos) { nNHStartPos = nStartPos - 1; nNHLen++; sInputStartDel = sParText.Substring(nNHStartPos, 1); } if (nStartPos + nWordLen < sParText.Length) { nNHLen++; sInputEndDel = sParText.Substring(nStartPos + nWordLen, 1); } if (sInputEndDel == "\r" || sInputEndDel == "\n") { sInputEndDel = ""; } string sInputNbrhood = sInputStartDel + sInput + sInputEndDel; bStart = false; j += 1; if (m_oHunspell != null && m_oEncoder != null) { if (sInput.Length > 0) { sUniInput = sInput; // apply Encode to get the string in good form ready to // apply the Spell function. The return value is stored // in the nRetFlag global variable for later use. nRetFlag = -1; ArrayList arrEncoded = new ArrayList(); nRetFlag = m_oEncoder.Encode_U(sInput, arrEncoded); if (arrEncoded.Count != 0) { sSCIIOutput = arrEncoded[0].ToString(); // only consider applying Spell if return is positive, // otherwise, skip the word and check the next word. if (nRetFlag > 0) { // lblEcnodeTxt.Text = (nRetFlag < 2) ? " ARMSCII-8 " : " UNICODE "; // check for accent and extract it. Note that sInput is used instead of encoded // sSCIIOutput, because in the case of ARMSCII-8 input text, the encode function // that is used strips the accent characters, although the other case does not string sAccent = ""; int nAccIndex = sInput.IndexOfAny(Globals.ThisAddIn.IsArmenianAccent); if (nAccIndex != -1) { string sAcc = sInput.Substring(nAccIndex, 1); if (nRetFlag < 2) { sAcc = Globals.ThisAddIn.DecodeAccent(sAcc); } if (sAcc.IndexOfAny(Globals.ThisAddIn.IsArmenianHyphen) == -1) { sAccent = sAcc; // strip from the encoded output only for UNICODE case, since in the // other case it is already stripped if (nRetFlag == 2) { sSCIIOutput = sSCIIOutput.Remove(nAccIndex, 1); } } } bool bLegal = m_oHunspell.Spell(sSCIIOutput); if (!bLegal) { lblCurrentWord.Text = sInput; // select the corresponding word in the source document Word.Range rg = CurRange; Word.Find fnd = rg.Find; fnd.Text = sInputNbrhood; fnd.Forward = true; fnd.ClearFormatting(); if (ExecuteFind(fnd)) { if (sInputStartDel.Length > 0) { rg.Start += 1; } if (sInputEndDel.Length > 0) { rg.End -= 1; } rg.Select(); SelWordRange = rg.Duplicate; } CurRange.Start = rg.End; List <string> suggests = m_oHunspell.Suggest(sSCIIOutput); if (suggests.Count > 0) { lstWords.Enabled = true; for (int i = 0; i < suggests.Count; i++) { string sAccentedWord = suggests[i]; if (sAccent.Length > 0) { sAccentedWord = Globals.ThisAddIn.PutAccent(sAccentedWord, sAccent); } lstWords.Items.Add(sAccentedWord); } lstWords.SelectedIndex = 0; lstWords.Focus(); } else { lstWords.Items.Add("(Ուղղագրիչը առաջարկ չունի)"); lstWords.Enabled = false; btnIgnore.Focus(); btnChange.Enabled = false; btnAdd.Enabled = true; return; } btnAdd.Enabled = btnChange.Enabled = lstWords.Enabled; } else { CheckNext_U(); } } else { CheckNext_U(); } } else { CheckNext_U(); } } } } else { if (bRestartPar) { RestartParagraph(); } else { nParIndex += 1; } if (nParIndex <= nParCount) { bStart = true; if (bCheckNormalText) { // search for the next offset of armenian character starting from current paragraph Word.Paragraph CurPar = oPars[nParIndex]; int nCurPos = CurPar.Range.Start; Object oRS = null, oRE = null; Word.Range rg = Globals.ThisAddIn.Application.ActiveDocument.Range(ref oRS, ref oRE); rg.End = int.MaxValue; // search for the first armenian character starting from the current cursor position nCurPos = SearchForArmenianChar(rg, nCurPos); if (nParIndex > nParCount) { bCheckNormalText = false; nShapeIndex++; RestartParagraph(); return; } } // continue with the resulting paragraph Word.Paragraph par = oPars[nParIndex]; words = par.Range.Words; nIParOffset = 0; CheckParagraph(par); } else { bCheckNormalText = false; nShapeIndex++; if (nShapeIndex <= oShapes.Count) { bStart = true; bCursorInShape = false; RestartParagraph(); } else { TerminateSpellCheck(); } } } }
//note that this saves existing highlight and highlights //this is because looping through document twice will increase runtime by 2n //where n is around 15 seconds per page \(*o*)/ private void SaveExistingAndHighlightRange(Word.Range range, LowerAndUpperBounds bounds) { //range to search int wordCount = range.Words.Count; //***lower bound for arrays is 1 in Word object model*** // (array[0] does exist but is not used) // (array[1] is the first word in the array) int lowerBoundWordCount = 1; int upperBoundWordCount = wordCount + 1; Word.Words words = range.Words; //we have two saved colors (one for continuing range of same color) //so we can bulk highlight a sequence of words with the same color //for faster runtime (updating word one by one takes too long) Word.WdColorIndex tempColor = Word.WdColorIndex.wdNoHighlight; Word.WdColorIndex tempColor2 = Word.WdColorIndex.wdNoHighlight; Word.Range tempRange = null; Globals.ThisAddIn.Application.System.Cursor = Word.WdCursorType.wdCursorWait; for (int i = lowerBoundWordCount; i < upperBoundWordCount; i++) { Word.Range wordRange = words[i]; //save existing highlight first if (wordRange.HighlightColorIndex != Colors.none) { this.SaveExistingHighlight(wordRange); } //get word string word = wordRange.Text; string cleanWord = CleanWord(word); //move page as necessary (kinda like progress bar but more intuitive) //right now, every 20 words if (i % 20 == 0) { wordRange.Select(); } /* * Periods, commas, etc are considered words * so skip through them if so. * However, for hyphens it may be one word * for example merry-go-round * or it may not be like -this hyphen acts as a bullet point. * Make an exception for hyphens and consider it as one word if necessary */ if (isPunctuation(cleanWord)) { if (tempRange == null) { continue; } //reset highlight color to nothing this.AddColoring(tempRange, tempColor); tempRange = null; tempColor = Word.WdColorIndex.wdNoHighlight; wordRange.HighlightColorIndex = Word.WdColorIndex.wdNoHighlight; continue; } //now highlight /* * For highlighting, looping word by word is extremely slow * 900 words about 1 minute * But highlighting the entire page a single color takes a second.. * So to lessen run time, bulk highlight words with same color. * */ if (Dictionary.ContainsKey(cleanWord)) { int wordIndex = Dictionary[cleanWord]; tempColor2 = bounds.GetColorOfIndex(wordIndex); } else { tempColor2 = Colors.unknown; UnknownWords.Add(cleanWord); } if (tempRange == null) //first instance { tempColor = tempColor2; tempRange = wordRange; } else { if (tempColor == tempColor2) //we continue to the next word { int rangeSize = wordRange.Characters.Count; tempRange.MoveEnd(Word.WdUnits.wdCharacter, rangeSize); } else //we highlight the range before { this.AddColoring(tempRange, tempColor); tempRange = wordRange; tempColor = tempColor2; } } } //end for loop of words Globals.ThisAddIn.Application.System.Cursor = Word.WdCursorType.wdCursorNormal; //pointer back to normal //highlight last set of words if (tempRange != null) //in case last word is a punctuation mark { this.AddColoring(tempRange, tempColor); } //remove selected text Globals.ThisAddIn.Application.Selection.Move(); //save range so the next time we highlight, //we can remove highlight on this range prevRange = range; }