Exemplo n.º 1
0
        public void InterpretHighlightTags(Word.Application appWord, Word.Document doc)
        {
            Word.Range        rng = doc.Content;
            Word.Find         f   = rng.Find;
            Word.WdColorIndex old = appWord.Options.DefaultHighlightColorIndex;

            appWord.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdYellow;
            f.Replacement.ClearFormatting();
            f.Replacement.Highlight = 1;
            FindAndReplace(doc, "\\[yellow\\](*)\\[/yellow\\]", f);

            appWord.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdBrightGreen;
            f.Replacement.ClearFormatting();
            f.Replacement.Highlight = 1;
            FindAndReplace(doc, "\\[brightgreen\\](*)\\[/brightgreen\\]", f);

            appWord.Options.DefaultHighlightColorIndex = Word.WdColorIndex.wdTurquoise;
            f.Replacement.ClearFormatting();
            f.Replacement.Highlight = 1;
            FindAndReplace(doc, "\\[t\\](*)\\[/t\\]", f);

            f.Replacement.ClearFormatting();
            f.Replacement.Font.StrikeThrough = 1;
            FindAndReplace(doc, "\\[s\\](*)\\[/s\\]", f);

            // reset options
            f.Replacement.Highlight = 0;
            appWord.Options.DefaultHighlightColorIndex = old;
        }
Exemplo n.º 2
0
        //set highlights back to original
        private void ReHighlight()
        {
            List <int> tempArray = new List <int>();

            for (int i = 0; i < SavedHighlights.Count; i++)
            {
                Tuple <Word.Range, Word.WdColorIndex> tuple = SavedHighlights[i];
                Word.Range        tRange     = tuple.Item1;
                Word.WdColorIndex tHighlight = tuple.Item2;

                //we should clear the range's highlight before calling this function
                if (tRange.HighlightColorIndex == Colors.none) //just in case
                {
                    tRange.HighlightColorIndex = tHighlight;
                    tempArray.Add(i);
                }
            }

            //loop backwards so we can remove elements as we go
            int tempCt = tempArray.Count - 1;

            for (int i = tempCt; i >= 0; i--)
            {
                SavedHighlights.RemoveAt(tempArray[i]);
            }
        }
Exemplo n.º 3
0
 public static void highlightText(Word.Range rng, string str, Word.WdColorIndex clr)
 {
     rng.Find.ClearFormatting();
     rng.Find.Forward = true;
     rng.Find.Text    = str;
     rng.Find.Execute();
     while (rng.Find.Found)
     {
         rng.HighlightColorIndex = clr;
         rng.Find.Execute();
     }
 }
Exemplo n.º 4
0
        private void lvSearchResult_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ClearMark();
            if (lvSearchResult.SelectedItems.Count > 0)
            {
                Word.Range range = lvSearchResult.SelectedItems[0].Tag as Word.Range;

                // 为了可以恢复被修改的Range,我先将该Range和原本的Color放入Class的成员
                _LastRange                = range;
                _LastRangeBackColor       = range.HighlightColorIndex;
                range.HighlightColorIndex = Word.WdColorIndex.wdYellow;
            }
        }
Exemplo n.º 5
0
        public static MSWord.Style CreateTableStyle(ref MSWord.Document wdDoc)
        {
            MSWord.WdBorderType verticalBorder = MSWord.WdBorderType.wdBorderVertical;
            MSWord.WdBorderType leftBorder     = MSWord.WdBorderType.wdBorderLeft;
            MSWord.WdBorderType rightBorder    = MSWord.WdBorderType.wdBorderRight;
            MSWord.WdBorderType topBorder      = MSWord.WdBorderType.wdBorderTop;

            MSWord.WdLineStyle doubleBorder = MSWord.WdLineStyle.wdLineStyleDouble;
            MSWord.WdLineStyle singleBorder = MSWord.WdLineStyle.wdLineStyleSingle;

            MSWord.WdTextureIndex noTexture = MSWord.WdTextureIndex.wdTextureNone;
            MSWord.WdColor        gray10    = MSWord.WdColor.wdColorGray10;
            MSWord.WdColor        gray70    = MSWord.WdColor.wdColorGray70;
            MSWord.WdColorIndex   white     = MSWord.WdColorIndex.wdWhite;

            object styleTypeTable = MSWord.WdStyleType.wdStyleTypeTable;

            MSWord.Style styl = wdDoc.Styles.Add("New Table Style", ref styleTypeTable);

            styl.Font.Name            = "Arial";
            styl.Font.Size            = 11;
            styl.Table.Borders.Enable = 1;

            MSWord.ConditionalStyle evenRowBanding = styl.Table.Condition(MSWord.WdConditionCode.wdEvenRowBanding);
            evenRowBanding.Shading.Texture = noTexture;
            evenRowBanding.Shading.BackgroundPatternColor = gray10;
            // Borders have to be set specifically for every condition.
            evenRowBanding.Borders[leftBorder].LineStyle     = doubleBorder;
            evenRowBanding.Borders[rightBorder].LineStyle    = doubleBorder;
            evenRowBanding.Borders[verticalBorder].LineStyle = singleBorder;

            MSWord.ConditionalStyle firstRow = styl.Table.Condition(MSWord.WdConditionCode.wdFirstRow);
            firstRow.Shading.BackgroundPatternColor = gray70;
            firstRow.Borders[leftBorder].LineStyle  = doubleBorder;
            firstRow.Borders[topBorder].LineStyle   = doubleBorder;
            firstRow.Borders[rightBorder].LineStyle = doubleBorder;
            firstRow.Font.Size       = 14;
            firstRow.Font.ColorIndex = white;
            firstRow.Font.Bold       = 1;

            // Set the number of rows to include in a "band".
            styl.Table.RowStripe = 1;
            return(styl);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 文書の末尾に注意事項を追加する.
        /// </summary>
        private static void AddNotes(Word.Application wordApp, ref Word.Document document, Word.WdColorIndex color, String text)
        {
            if (!String.IsNullOrEmpty(text))
            {
                // 末尾にテキスト挿入
                Int32      before = GetLastPosition(ref document);
                Word.Range rng    = document.Range(document.Content.End - 1, document.Content.End - 1);
                rng.Text += text;
                Int32 after = GetLastPosition(ref document);

                // フォント色設定
                document.Range(before, after).Font.ColorIndex = color;

                // 改行を追加
                AddParagraph(wordApp, ref document);

                // 外枠設定
                document.Range(before, document.Content.End - 1).Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 文書の末尾にテキストを追加する.
        /// </summary>
        private static void AddText(Word.Application wordApp, ref Word.Document document, Word.WdColorIndex color, String text)
        {
            if (!String.IsNullOrEmpty(text))
            {
                Int32      before = GetLastPosition(ref document);
                Word.Range rng    = document.Range(document.Content.End - 1, document.Content.End - 1);
                rng.Text += text;
                Int32 after = GetLastPosition(ref document);

                // フォント色設定
                document.Range(before, after).Font.ColorIndex = color;

                // 改行を追加
                AddParagraph(wordApp, ref document);
                AddParagraph(wordApp, ref document);
            }
        }
Exemplo n.º 8
0
 private void AddColoring(Word.Range range, Word.WdColorIndex color)
 {
     range.HighlightColorIndex = color;
 }
Exemplo n.º 9
0
        //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;
        }
Exemplo n.º 10
0
        private void CheckWords(Word.Document doc, Hunspell hunspell, string selectedText, string[] khmerwords, out bool stopcheck, out bool repeatcheck, string objecttype = "", object wordobject = null)
        {
            int    startposition = 0;
            Object oMissing      = System.Reflection.Missing.Value;

            stopcheck = repeatcheck = false;

            //Check all the Khmer words from the selected line
            foreach (string khmerword in khmerwords)
            {
                DialogResult dialogResult = DialogResult.None;
                frmKhmer     frmKhmer     = null;
                String       newKhmerWord = String.Empty;

                if (!hunspell.Spell(khmerword))
                {
                    if (!ignoreAllWords.Any(ignoreAllWord => ignoreAllWord.khmerword == khmerword))
                    {
                        if (!ignoreWords.Contains(new IgnoreWord {
                            document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition, ignoreAll = false
                        }))
                        {
                            Word.Range        start = null;
                            Word.WdColorIndex highlightcolorindex = Word.WdColorIndex.wdNoHighlight;
                            Word.WdUnderline  fontunderline       = Word.WdUnderline.wdUnderlineNone;
                            Word.WdColor      fontcolor           = Word.WdColor.wdColorBlack;
                            Word.Range        selectionRange      = null;

                            //Select the erroneous word on the main document
                            if (String.IsNullOrWhiteSpace(objecttype))
                            {
                                //Set the initial selection
                                start = doc.ActiveWindow.Selection.Range;

                                //Set the search area
                                doc.ActiveWindow.Selection.Start += startposition;
                                Word.Selection searchArea = doc.ActiveWindow.Selection;

                                //Set the find object
                                Word.Find findObject = searchArea.Find;
                                findObject.ClearFormatting();
                                findObject.Text = khmerword;


                                //Find the mis-spelled word
                                findObject.Execute(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, ref oMissing, ref oMissing);

                                //Temp store the current formatting
                                highlightcolorindex = doc.ActiveWindow.Selection.Range.HighlightColorIndex;
                                fontunderline       = doc.ActiveWindow.Selection.Range.Font.Underline;
                                fontcolor           = doc.ActiveWindow.Selection.Range.Font.UnderlineColor;

                                //Highlight the selection
                                doc.ActiveWindow.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdYellow;
                                doc.ActiveWindow.Selection.Range.Font.Underline      = Word.WdUnderline.wdUnderlineWavy;
                                doc.ActiveWindow.Selection.Range.Font.UnderlineColor = Word.WdColor.wdColorRed;
                                selectionRange = doc.ActiveWindow.Selection.Range;
                                doc.ActiveWindow.Selection.Collapse();
                            }
                            else
                            {
                                if (objecttype == "table")
                                {
                                    start = ((Word.Cell)wordobject).Range;
                                }
                                else if (objecttype == "shape")
                                {
                                    start        = ((Word.Shape)wordobject).TextFrame.TextRange;
                                    start.Start += startposition;
                                }

                                //Set the find object
                                Word.Find findObject = start.Find;
                                findObject.ClearFormatting();
                                findObject.Text = khmerword;

                                //Temp store the current formatting
                                highlightcolorindex = start.HighlightColorIndex;
                                fontunderline       = start.Font.Underline;
                                fontcolor           = start.Font.UnderlineColor;

                                //Find the mis-spelled word
                                findObject.Execute(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, ref oMissing, ref oMissing);

                                //Highlight the selection
                                start.HighlightColorIndex = Word.WdColorIndex.wdYellow;
                                start.Font.Underline      = Word.WdUnderline.wdUnderlineWavy;
                                start.Font.UnderlineColor = Word.WdColor.wdColorRed;
                                start.Select();
                            }

                            bool isObject = !String.IsNullOrWhiteSpace(objecttype);
                            frmKhmer     = new frmKhmer(selectedText, khmerword, startposition, hunspell.Suggest(khmerword), isObject);
                            dialogResult = frmKhmer.ShowDialog();

                            //Select the line again
                            if (String.IsNullOrWhiteSpace(objecttype))
                            {
                                //Revert the highlights
                                selectionRange.Select();
                                doc.ActiveWindow.Selection.Range.HighlightColorIndex = highlightcolorindex;
                                doc.ActiveWindow.Selection.Range.Font.Underline      = fontunderline;
                                doc.ActiveWindow.Selection.Range.Font.UnderlineColor = fontcolor;

                                if (dialogResult != DialogResult.Abort)
                                {
                                    start.Select();
                                }
                            }
                            else
                            {
                                start.HighlightColorIndex = highlightcolorindex;
                                start.Font.Underline      = fontunderline;
                                start.Font.UnderlineColor = fontcolor;

                                if (dialogResult != DialogResult.Abort)
                                {
                                    if (objecttype == "table")
                                    {
                                        ((Word.Cell)wordobject).Select();
                                    }
                                    else if (objecttype == "shape")
                                    {
                                        ((Word.Shape)wordobject).Select();
                                    }
                                }
                            }
                        }
                    }
                }

                #region Cancel Button Clicked
                //Return if the user hits Cancel Button
                if (dialogResult == DialogResult.Cancel || dialogResult == DialogResult.Abort)
                {
                    stopcheck   = true;
                    repeatcheck = false;
                    return;
                }
                #endregion

                #region Ignore or Ignore All Clicked
                //Ignore the word
                if (dialogResult == DialogResult.Ignore)
                {
                    if (frmKhmer.ignoreAll)
                    {
                        ignoreAllWords.Add(new IgnoreWord {
                            khmerword = khmerword, ignoreAll = frmKhmer.ignoreAll
                        });
                    }
                    else
                    {
                        ignoreWords.Add(new IgnoreWord {
                            document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition
                        });
                    }
                }
                #endregion

                #region Change or Change All Clicked
                if (dialogResult == DialogResult.Yes)
                {
                    if (String.IsNullOrWhiteSpace(objecttype))
                    {
                        //Set the initial selection
                        Word.Range start = doc.ActiveWindow.Selection.Range;

                        //Set the searcharea
                        if (frmKhmer.changeAll)
                        {
                            doc.Content.Select();
                        }
                        Word.Selection searchArea = doc.ActiveWindow.Selection;

                        //Set the find object
                        Word.Find findObject = searchArea.Find;
                        findObject.ClearFormatting();
                        findObject.Text = khmerword;
                        findObject.Replacement.ClearFormatting();
                        findObject.Replacement.Text = frmKhmer.selectedSuggestion;

                        object replaceAll = frmKhmer.changeAll ? Word.WdReplace.wdReplaceAll : Word.WdReplace.wdReplaceOne;

                        findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                           ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                           ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                        newKhmerWord = frmKhmer.selectedSuggestion;

                        //Set back the selection
                        start.Select();

                        //Set repeatcheck to true
                        if (frmKhmer.changeAll)
                        {
                            stopcheck   = false;
                            repeatcheck = true;
                            return;
                        }
                    }
                    else
                    {
                        var resultingText = selectedText.Replace(khmerword, frmKhmer.selectedSuggestion);

                        if (objecttype == "table")
                        {
                            Word.Range range = ((Word.Cell)wordobject).Range;
                            range.Text = resultingText;
                        }
                        else if (objecttype == "shape")
                        {
                            Word.Shape shape = (Word.Shape)wordobject;
                            shape.TextFrame.TextRange.Text = resultingText;
                        }

                        stopcheck   = false;
                        repeatcheck = true;
                        return;
                    }
                }
                #endregion

                startposition += String.IsNullOrWhiteSpace(newKhmerWord) ? khmerword.Length : newKhmerWord.Length;
            }
        }