Exemplo n.º 1
0
        private List <Range> GetFoundRanges(SearchWordsForm statusForm, Document document, List <string> words)
        {
            List <Range> foundRanges = new List <Word.Range>();
            Range        range;
            int          i = 0;

            foreach (string word in words)
            {
                i++;
                statusForm.PercentageLabel.Text = string.Format("{0}%", (int)(100.0 * ((double)i) / words.Count));
                statusForm.SearchForLabel.Text  = word;
                statusForm.Update();

                range = document.Content;
                range.Find.Forward        = true;
                range.Find.Wrap           = Word.WdFindWrap.wdFindStop;
                range.Find.Text           = word;
                range.Find.MatchWholeWord = !partCheckBox.Checked;

                while (range.Find.Execute())
                {
                    foundRanges.Add(range.Duplicate);
                }
            }

            return(foundRanges);
        }
Exemplo n.º 2
0
        private void CheckGenderButton_Click(object sender, RibbonControlEventArgs e)
        {
            SearchWordsForm statusForm = null;

            try
            {
                ThisAddIn.MyApplication.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

                Word.Document document = ThisAddIn.MyApplication.ActiveDocument;

                if (!RemoveGenderingFields())
                {
                    return;
                }

                statusForm = new SearchWordsForm();
                statusForm.Show();

                XmlDocument   genderingTable = GenderingEngine.GetGenderingTable();
                List <string> words          = GetWords(genderingTable);

                List <Word.Range> foundRanges = GetFoundRanges(statusForm, document, words);

                bool switchToFormFillIn = false;

                foreach (Word.Range foundRange in foundRanges)
                {
                    List <string> alternatives = GetAlternatives(genderingTable, foundRange.Text.ToLowerInvariant());

                    Trace.WriteLine(foundRange.Start);
                    Trace.WriteLine(foundRange.End);

                    bool alreadyGendered = false;
                    foreach (string alternative in alternatives)
                    {
                        if (IsAlreadyGendered(document, foundRange, alternative))
                        {
                            alreadyGendered = true;
                            break;
                        }
                    }

                    if (alternatives.Count != 0 && !alreadyGendered)
                    {
                        CreateDropDown(document, foundRange, alternatives);
                        switchToFormFillIn = true;
                    }
                }

                if (switchToFormFillIn && Is97(document))
                {
                    document.Protect(WdProtectionType.wdAllowOnlyFormFields);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Fehler", MessageBoxButtons.OK);
            }
            finally
            {
                if (statusForm != null)
                {
                    statusForm.Close();
                }

                ThisAddIn.MyApplication.DisplayAlerts = Word.WdAlertLevel.wdAlertsMessageBox;
            }
        }