Exemplo n.º 1
0
        };                                  // there are about 30 other quote characters, that might appear in different languages

        public void ReplaceSmartQuotesInSelection()
        {
            Word.UndoRecord ur = wordApp.UndoRecord;
            ur.StartCustomRecord("Code Indentation");

            bool smartQuotesAsYouType_old = wordApp.Application.Options.AutoFormatAsYouTypeReplaceQuotes;
            bool smartQuotes_old          = wordApp.Application.Options.AutoFormatReplaceQuotes;

            wordApp.Application.Options.AutoFormatAsYouTypeReplaceQuotes = false;
            wordApp.Application.Options.AutoFormatReplaceQuotes          = false;

            /* If there is no selection, then Selection.Find searches the entire document!
             * Selection.Text contains one character if only one character is selected of course, but also if there is nothing selected at all!
             * The only thing distinguishing these two completely different states is Selection.Range: if 1 character is selected it has a Range.Text object, if nothing is selected it doesn't have one
             * Other not documented workings of the Word API: if only one character is selected, and it matches the search criteria, then the search
             * continues through the entire document, even if it's explicitely set that it has to stop at the end of the selection. */
            if (wordApp.Selection.Range.Text != null)
            {
                if (wordApp.Selection.Text.Length == 1)
                {
                    ReplaceQuotes_OneCharacterSelected(quotes);
                }
                else
                {
                    ReplaceQuotes_Deafult(quotes);
                }
            }

            wordApp.Application.Options.AutoFormatAsYouTypeReplaceQuotes = smartQuotesAsYouType_old;
            wordApp.Application.Options.AutoFormatReplaceQuotes          = smartQuotes_old;
            ur.EndCustomRecord();
        }
Exemplo n.º 2
0
 private void AddZhuyin_Click(object sender, RibbonControlEventArgs e)
 {
     Word.UndoRecord undorecord = Globals.ThisAddIn.Application.UndoRecord; // This doesn't work
     undorecord.StartCustomRecord("Add Zhuyin");
     pinyintones(tghz.withZhuyinXMLRuby);
     resizepinyin_Click(sender, e);
     undorecord.EndCustomRecord();
 }
Exemplo n.º 3
0
        public static void ConvertAllUrlsToHyperlinks(Word.Application wordApp)
        {
            if (wordApp.ActiveDocument == null)
            {
                return;
            }

            wordApp.ScreenUpdating = false;
            Word.UndoRecord ur = wordApp.UndoRecord;
            ur.StartCustomRecord("Convert all URLs to Hyperlinks");

            try
            {
                string   fulltext = wordApp.ActiveDocument.Content.Text; // this could be enormous, but processing it paragraph by pharagraph is too slow.
                string[] urls     = UrlDetection.FindAllUrls(fulltext);
                Array.Sort(urls, ComapreStringsByLength);

                object missing = System.Type.Missing;
                for (int i = 0; i < urls.Length; i++)
                {
                    Word.Range content = wordApp.ActiveDocument.Content;
                    content.Find.ClearFormatting();
                    content.Find.Forward = false; // true causes an infinite cycle, yet that's what the example uses on msdn
                    content.Find.Text    = urls[i];

                    content.Find.Execute(
                        ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing);

                    while (content.Find.Found)
                    {
                        if (content.Hyperlinks.Count == 0)
                        {
                            string url = urls[i];
                            if (!url.StartsWith("http://") && !url.StartsWith("https://"))
                            {
                                url = url.Insert(0, "http://");
                            }

                            content.Hyperlinks.Add(content, url, null, url, missing, missing);
                        }

                        content.Find.Execute(
                            ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing);
                    }
                }
            }
            catch (Exception e) { }
            finally // The whole thing is only needed to make sure this runs (ScreenUpdate can get stuck)
            {
                wordApp.ScreenUpdating = true;
                ur.EndCustomRecord();
            }
        }
Exemplo n.º 4
0
        public void Indent(int strategy)
        {
            Word.UndoRecord ur = wordApp.UndoRecord;
            ur.StartCustomRecord("Code Indentation");
            //wordApp.ScreenUpdating = false; // could be useful for slow computers

            if (wordApp.Selection.Range.Text != null)
            {
                wordApp.Selection.NoProofing = 1; // spell checks are usually just add noise to source code samples in Word Documents
                IndentContent(strategy);
            }

            wordApp.ScreenUpdating = true;
            ur.EndCustomRecord();
        }
Exemplo n.º 5
0
        public static void RemoveAllHyperlinks(Word.Application wordApp)
        {
            Word.Document doc = wordApp.ActiveDocument;
            if (wordApp.ActiveDocument == null ||
                wordApp.ActiveDocument.Hyperlinks == null ||
                wordApp.ActiveDocument.Hyperlinks.Count == 0)
            {
                return;
            }

            wordApp.ScreenUpdating = false;
            Word.UndoRecord ur = wordApp.UndoRecord;
            ur.StartCustomRecord("Remove all URL Hyperlinks");
            try
            {
                // It needs a copy of the list, because I can't remove anything from the original while iterating it
                Word.Hyperlink[] links = new Word.Hyperlink[doc.Hyperlinks.Count];
                for (int i = 0; i < doc.Hyperlinks.Count; i++)
                {
                    links[i] = doc.Hyperlinks[i + 1];
                }

                for (int i = 0; i < links.Length; i++)
                {
                    string address;
                    try
                    {
                        address = links[i].Address;
                    }
                    catch (Exception e) // for this same reason as above
                    {
                        address = null;
                    }
                    if (!String.IsNullOrWhiteSpace(address) &&
                        (address.StartsWith("http://") || address.StartsWith("https://")))
                    {
                        links[i].Delete();
                    }
                }
            }
            catch (Exception e) { }
            finally // The whole thing is only needed to make sure this runs (ScreenUpdate can get stuck)
            {
                wordApp.ScreenUpdating = true;
                ur.EndCustomRecord();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Colorizes the selected text using the selected programming language. This method is usually called when the user clicks on the Colorize button.
        /// The method creates the pharagraph formatting of the text, and then redirects to the Colorizer to determine the syntax based coloring. The Colorizer will then call back the Formatter to color individual words or characters.
        /// </summary>
        /// <param name="language">The programming language that the selected text is written in</param>
        /// <param name="use_frame">Determines whether the code block will be put in a frame</param>
        /// <param name="use_linenumbering">Determines whether line numbering should be used</param>
        public void Colorize(string language, bool use_frame, bool use_linenumbering)
        {
            if (wordApp.Selection.Text.Length > 1) // If there is nothing selected, this property still contains 1 character.
            {
                if (TooLargeInput(use_frame, use_linenumbering, wordApp.Selection.Paragraphs.Count))
                {
                    return;
                }

                // Using an UndoRecord enables the user the "undo" all the coloring at once. Without it every formatting action would be an individual element in the undo stack.
                Word.UndoRecord ur = wordApp.UndoRecord;
                ur.StartCustomRecord("Syntax Highlighting");
                //wordApp.ScreenUpdating = false; // might be useful on slow computers?

                // Vertical tab characters are replaced with newlines, as they would usually mess up the pharagraph formatting
                if (wordApp.Selection.Text.Contains("\x0B"))
                {
                    string temp = wordApp.Selection.Text.Replace("\x0B", "\n");
                    wordApp.Selection.Text = temp.Remove(temp.Length - 1); // Replace inserts a '\n' at the end that must be removed
                }

                wordApp.Selection.ClearFormatting(); // All previous formatting is removed! Otherwise the result of the formatting could be quiet unpredictable
                wordApp.Selection.NoProofing = 1;    // Spell checks usually just add noise to a code block in a document
                wordApp.Selection.Font.Name  = this.fontName;
                wordApp.Selection.Font.Size  = this.fontSize;

                try
                {
                    // Calculates what colors should be used for each word or character, and calls back the formatter
                    colorizer.Colorize(this, language, wordApp.Selection.Text);
                }
                catch (InvalidLanguageException e)
                {
                    MessageBox.Show(e.Message, "Syntax Highlighter Error");
                }
                catch (InvalidRegexException e)
                {
                    MessageBox.Show(e.Message, "Syntax Highlighter Error");
                }

                this.ParagraphFormatting(use_frame, use_linenumbering);

                wordApp.ScreenUpdating = true;
                ur.EndCustomRecord();
            }
        }
        private void insTblButton_Click(object sender, EventArgs e)
        {
            Word.UndoRecord undorecord = Globals.ThisAddIn.Application.UndoRecord;
            undorecord.StartCustomRecord("Add WordList as Table");
            {
                Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
                Word.Table tbl          = currentRange.Tables.Add(currentRange, 1, 2);

                int i = 0;
                foreach (Tuple <Chinese, List <Pinyin> > word in set)
                {
                    i++; tbl.Rows.Add(); // Add a second Empty extra row
                    foreach (Pinyin pinyin in word.Item2)
                    {
                        tbl.Cell(i, 1).Range.Text         = word.Item1;
                        tbl.Cell(i, 1).PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthAuto;
                        Word.Range rng = tbl.Cell(i, 2).Range;
                        foreach (PinyinChar pychar in pinyin)
                        {
                            if (pinyinradio.Checked)
                            {
                                rng.Text = pychar.withDiacritic() + " ";
                            }
                            else
                            {
                                rng.Text = pychar.toZhuYin(zhuyindict) + " ";
                            }
                            Color colour = System.Drawing.ColorTranslator.FromHtml("#" + colours[pychar.tone - 1]);
                            rng.Font.TextColor.RGB = colour.B * 0x010000 + colour.G * 0x0100 + colour.R;
                            rng.Start = rng.End - 1;
                        }
                    }
                }

                tbl.Rows[i + 1].Delete(); // Delete Extra Last row
                tbl.Columns.DistributeWidth();
            }
            undorecord.EndCustomRecord();
        }
Exemplo n.º 8
0
        private void button_ReplaceAll_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(richTextBox_Regex.Text) || wordApp.ActiveDocument == null)
            {
                return;
            }

            Regex regex = CreateRegex();

            if (regex != null)
            {
                wordApp.ScreenUpdating = false;
                Word.UndoRecord ur = wordApp.UndoRecord;
                ur.StartCustomRecord("Convert all URLs to Hyperlinks");
                replaced_cntr       = 0;
                previousFindResult  = null;
                previousReplacement = "";
                reachedEnd          = false;

                try
                {
                    MoveCursorToBeginningOfDocument();
                    while (!reachedEnd)
                    {
                        Replace(regex);
                    }
                }
                finally
                {
                    previousFindResult  = null;
                    previousReplacement = "";
                    reachedEnd          = false;
                    replaced_cntr       = 0;
                    ur.EndCustomRecord();
                    wordApp.ScreenUpdating = true;
                }
            }
        }