示例#1
0
        void Application_WindowBeforeRightClick(Word.Selection Sel, ref bool Cancel)
        {
            clickNumber = 0;
            Word.Document document = this.Application.ActiveDocument; // 全文文档对象
            lineQuestion = new LineQuestion();
            lineQuestion.leftAnswerList = new List<LineLeftAnswer>();
            lineQuestion.rightAnswerList = new List<LineRightAnswer>();
            //获取标签为"autoAnswer"的菜单项
            Office.CommandBarButton addBtn = (Office.CommandBarButton)Application.CommandBars["Text"].FindControl(Office.MsoControlType.msoControlButton, missing, "autoAnswer", false);
            addBtn.Enabled = false;

            //addBtn.Click -= new Office._CommandBarButtonEvents_ClickEventHandler(_RightBtn_Click);


            if (!string.IsNullOrWhiteSpace(Sel.Range.Text) && Sel.Range.Text.Trim().Length >= 2 && Sel.Range.Text.Trim().Length <= 5 && Sel.Range.Text.Trim().Contains("连线"))
            {
                addBtn.Enabled = true;

                //object charUnit = Word.WdUnits.wdCharacter; //字符移动
                object lineUnit = Word.WdUnits.wdLine; //行移动
                //object paragraphUnit = Word.WdUnits.wdParagraph; //段落移动
                //object storyUnit = Word.WdUnits.wdStory; // 当前文档
                //object count = 3;  // 移动次数
                object extend = Word.WdMovementType.wdExtend; //extend对光标移动区域进行扩展选择

                Sel.HomeKey(lineUnit);
                Sel.EndKey(lineUnit, extend);
                //选择光标所在段落的内容
                //Sel.MoveUp(paragraphUnit);
                //Sel.MoveDown(paragraphUnit, extend); 

                string currentParagraphText = Sel.Range.Text; // 获取该行文本内容
                lineQuestion.currentParagraph = 0;
                for (int i = 1; i <= document.Paragraphs.Count; i++)
                {
                    if (currentParagraphText.Trim().Equals(document.Paragraphs[i].Range.Text.Trim().ToString()))
                    {
                        // 获取该行在文档中的段落数
                        lineQuestion.currentParagraph = i;
                    }
                }
                if (lineQuestion.currentParagraph != 0)
                {
                    // 获取每一道连线题的连线行总数
                    lineQuestion.answerCount = validParagraphs(lineQuestion.currentParagraph);
                }
                if (lineQuestion.answerCount != 0)
                {

                    for (int i = 1; i <= lineQuestion.answerCount; i++)
                    {
                        // 获取连线行内容
                        string paragraphText = document.Paragraphs[lineQuestion.currentParagraph + i].Range.Text.ToString();

                        // 操作连线行左侧
                        string leftAnswerStr = paragraphText.Substring(0, paragraphText.IndexOf(" "));
                        LineLeftAnswer lla = new LineLeftAnswer();
                        lla.leftIndex = leftAnswerStr.Substring(0, leftAnswerStr.IndexOf("."));
                        lla.leftAnswer = leftAnswerStr.Substring(leftAnswerStr.IndexOf(".") + 1);
                        lineQuestion.leftAnswerList.Add(lla);

                        // 操作连线行左侧
                        string rightAnswerStr = paragraphText.Substring(paragraphText.LastIndexOf(" ") + 1);
                        LineRightAnswer lra = new LineRightAnswer();
                        lra.rightIndex = rightAnswerStr.Substring(0, rightAnswerStr.IndexOf("."));
                        lra.rightAnswer = rightAnswerStr.Substring(rightAnswerStr.IndexOf(".") + 1);
                        lineQuestion.rightAnswerList.Add(lra);
                    }
                }
                addBtn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(_RightBtn_Click);
            }
        }
示例#2
0
        void ThisDocument_BeforeDoubleClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
        {
            Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
            lineQuestion = new LineQuestion();
            lineQuestion.leftAnswerList = new List<LineLeftAnswer>();
            lineQuestion.rightAnswerList = new List<LineRightAnswer>();
            Word.Selection Sel = this.Application.Selection;
            object charUnit = Word.WdUnits.wdCharacter; //字符移动
            object lineUnit = Word.WdUnits.wdLine; //行移动
            object count = 5;  // 移动次数
            object extend = Word.WdMovementType.wdExtend; //extend对光标移动区域进行扩展选择
            Sel.MoveEnd(charUnit, count);
            Sel.HomeKey(lineUnit, extend);
            if (!string.IsNullOrWhiteSpace(Sel.Range.Text) && Sel.Range.Text.Trim().ToString().Contains("连线"))
            {
                Sel.HomeKey(lineUnit);
                Sel.EndKey(lineUnit, extend);
                string currentParagraphText = Sel.Range.Text; // 获取该行文本内容
                lineQuestion.currentParagraph = 0;
                for (int i = 1; i <= vstoDoc.Paragraphs.Count; i++)
                {
                    if (currentParagraphText.Trim().Equals(vstoDoc.Paragraphs[i].Range.Text.Trim().ToString()))
                    {
                        // 获取该行在文档中的段落数
                        lineQuestion.currentParagraph = i;
                        break;
                    }
                }
                if (lineQuestion.currentParagraph != 0)
                {
                    // 获取每一道连线题的连线行总数
                    lineQuestion.answerCount = validParagraphs(lineQuestion.currentParagraph);
                }
                if (lineQuestion.answerCount != 0)
                {

                    for (int i = 1; i <= lineQuestion.answerCount; i++)
                    {
                        // 获取连线行内容
                        string paragraphText = vstoDoc.Paragraphs[lineQuestion.currentParagraph + i].Range.Text.ToString();

                        // 操作连线行左侧
                        string leftAnswerStr = paragraphText.Substring(0, paragraphText.IndexOf(" "));
                        LineLeftAnswer lla = new LineLeftAnswer();
                        lla.leftIndex = leftAnswerStr.Substring(0, leftAnswerStr.IndexOf("."));
                        lla.leftAnswer = leftAnswerStr.Substring(leftAnswerStr.IndexOf(".") + 1);
                        lineQuestion.leftAnswerList.Add(lla);

                        // 操作连线行左侧
                        string rightAnswerStr = paragraphText.Substring(paragraphText.LastIndexOf(" ") + 1);
                        LineRightAnswer lra = new LineRightAnswer();
                        lra.rightIndex = rightAnswerStr.Substring(0, rightAnswerStr.IndexOf("."));
                        lra.rightAnswer = rightAnswerStr.Substring(rightAnswerStr.IndexOf(".") + 1);
                        lineQuestion.rightAnswerList.Add(lra);
                    }
                }

                if(lineQuestion != null)
                {
                    LineSelectAnswerForm answerForm = new LineSelectAnswerForm(lineQuestion) { StartPosition = FormStartPosition.CenterParent };
                    answerForm.AnswerEvent += (str) => { answer = str; }; // 获得答案
                    answerForm.ShowDialog();

                    Word.Document document = this.Application.ActiveDocument;
                    string originalTxt = document.Paragraphs[lineQuestion.currentParagraph].Range.Text.ToString();
                    originalTxt = originalTxt.Substring(0, originalTxt.IndexOf("]", 5) + 1);
                    string currentTxt = originalTxt + answer + "\n";
                    answer = null;
                    document.Paragraphs[lineQuestion.currentParagraph].Range.Text = currentTxt; // 替换原段落的内容
                }

            }
        }