public void SetTextInCell(int Column, int Row, string strText, int indTable = -1, string strDefault = "", bool bAdd = false) { if (strText == "") { strText = strDefault; if (strText == "") { return; } } int top = 1; int left = 1; //MSWord.Columns pCols = null; //MSWord.Rows pRows = null; MSWord.Cell pCell = null; MSWord.Selection pSel = null; GetTableReference(indTable); //pCols = m_pTable.Columns; //if (pCols == null) return; //pRows = m_pTable.Rows; //if (pRows == null) return; if (Column >= 0) { left = Column; } if (Row >= 0) { top = Row; } // проверяем правильность установки границ диапазона // получаем верхнюю левую ячейку диапазона pCell = m_pTable.Cell(top, left); if (pCell == null) { return; } if (!bAdd) { pCell.Range.Text = strText; } else { pCell.Select(); pSel = m_pApp.Selection; pSel.EndKey(); pSel.Text = strText; pSel.Collapse(0); } }
private void Print_Jornal() { // Инициализация Word this.Cursor = Cursors.WaitCursor; Word.Application o_Word = new Word.Application(); Word.Document o_Doc = new Word.Document(); string d_PrintName = o_Word.ActivePrinter; string n_PrintName = comboBox1.SelectedItem.ToString(); if (chkVisible.Checked) { o_Word.Visible = false; } else { o_Word.Visible = true; } string path_toFile = txtJornal.Text + "\\" + comboBox2.SelectedItem.ToString(); string dt_pick = dTPicker.Value.ToString("dd MMMM yyyy г."); object fileNameJorn = path_toFile; Object confirmConversions = Type.Missing; Object readOnly = Type.Missing; Object addToRecentFiles = Type.Missing; Object passwordDocument = Type.Missing; Object passwordTemplate = Type.Missing; Object revert = Type.Missing; Object writePasswordDocument = Type.Missing; Object writePasswordTemplate = Type.Missing; Object format = Type.Missing; Object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingOEMCyrillicII; Object visible = Type.Missing; Object openConflictDocument = Type.Missing; Object openAndRepair = Type.Missing; Object documentDirection = Type.Missing; Object noEncodingDialog = Type.Missing; o_Word.Documents.Open(ref fileNameJorn, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair, ref documentDirection, ref noEncodingDialog); o_Doc = o_Word.Documents.Application.ActiveDocument; Word.Selection o_Sel = o_Word.Selection; o_Sel.HomeKey(Unit: Word.WdUnits.wdStory, Extend: Word.WdMovementType.wdMove); o_Sel.EndKey(Unit: Word.WdUnits.wdLine, Extend: Type.Missing); o_Sel.TypeText(" " + dt_pick); o_Word.ActivePrinter = n_PrintName; Object background = Type.Missing; Object append = Type.Missing; Object range = Type.Missing; Object outputFileName = Type.Missing; Object from = Type.Missing; Object to = Type.Missing; Object item = Type.Missing; Object copies = Type.Missing; Object pages = Type.Missing; Object pageType = Type.Missing; Object printToFile = Type.Missing; Object collate = Type.Missing; Object fileName = Type.Missing; Object activePrinterMacGX = Type.Missing; Object manualDuplexPrint = Type.Missing; Object printZoomColumn = Type.Missing; Object printZoomRow = Type.Missing; Object printZoomPaperWidth = Type.Missing; Object printZoomPaperHeight = Type.Missing; o_Doc.PrintOut(ref background, ref append, ref range, ref outputFileName, ref from, ref to, ref item, ref copies, ref pages, ref pageType, ref printToFile, ref collate, ref activePrinterMacGX, ref manualDuplexPrint, ref printZoomColumn, ref printZoomRow, ref printZoomPaperWidth, ref printZoomPaperHeight); o_Word.ActivePrinter = d_PrintName; Thread.Sleep(2000); Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; Object originalFormat = Type.Missing; Object routeDocument = Type.Missing; ((Word._Application)o_Word).Quit(ref saveChanges, ref originalFormat, ref routeDocument); o_Word = null; this.Cursor = Cursors.Default; tSSlb.Text = "Файл " + comboBox2.SelectedItem.ToString() + " отправлен на " + n_PrintName; }
public void Format_vipLS() { // Инициализация Word this.Cursor = Cursors.WaitCursor; Word.Application o_Word = new Word.Application(); Word.Document o_Doc = new Word.Document(); string d_PrintName = o_Word.ActivePrinter; string n_PrintName = comboBox1.SelectedItem.ToString(); if (chkVisible.Checked) { o_Word.Visible = false; } else { o_Word.Visible = true; } //Открытие и редактирование документа Object filename = txtFrom.Text + "\\vips.txt"; Object confirmConversions = Type.Missing; Object readOnly = Type.Missing; Object addToRecentFiles = Type.Missing; Object passwordDocument = Type.Missing; Object passwordTemplate = Type.Missing; Object revert = Type.Missing; Object writePasswordDocument = Type.Missing; Object writePasswordTemplate = Type.Missing; Object format = Type.Missing; Object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingOEMCyrillicII; Object visible = Type.Missing; Object openConflictDocument = Type.Missing; Object openAndRepair = Type.Missing; Object documentDirection = Type.Missing; Object noEncodingDialog = Type.Missing; if (!System.IO.File.Exists(filename.ToString())) { o_Word.Visible = true; MessageBox.Show("Файл " + filename.ToString() + " не найден.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } o_Word.Documents.Open(ref filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair, ref documentDirection, ref noEncodingDialog); o_Doc = o_Word.Documents.Application.ActiveDocument; //o_Doc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA3; o_Doc.PageSetup.PageWidth = o_Word.CentimetersToPoints(29.7f); o_Doc.PageSetup.PageHeight = o_Word.CentimetersToPoints(42f); o_Doc.PageSetup.LeftMargin = o_Word.CentimetersToPoints(1.27f); o_Doc.PageSetup.RightMargin = o_Word.CentimetersToPoints(1.27f); o_Doc.PageSetup.TopMargin = o_Word.CentimetersToPoints(1.27f); o_Doc.PageSetup.BottomMargin = o_Word.CentimetersToPoints(1.27f); Word.Find o_Find = o_Word.Selection.Find; Word.Selection o_Sel = o_Word.Selection; o_Sel.WholeStory(); o_Sel.Font.Size = 9; o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); Object p_findText = "Нереализованная отриц."; if (o_Find.Execute(ref p_findText, MatchCase: false, MatchWholeWord: false, MatchWildcards: false, MatchSoundsLike: Type.Missing, MatchAllWordForms: false, Forward: true, Wrap: Type.Missing, Format: false, ReplaceWith: Type.Missing, Replace: Type.Missing)) { o_Sel.EndKey(Unit: Word.WdUnits.wdLine, Extend: Type.Missing); o_Sel.HomeKey(Unit: Word.WdUnits.wdStory, Extend: Word.WdMovementType.wdExtend); o_Sel.Delete(); o_Sel.Delete(); } else { o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); Object s_findText = "Расходы по процентам"; if (o_Find.Execute(ref s_findText, MatchCase: false, MatchWholeWord: false, MatchWildcards: false, MatchSoundsLike: Type.Missing, MatchAllWordForms: false, Forward: true, Wrap: Type.Missing, Format: false, ReplaceWith: Type.Missing, Replace: Type.Missing)) { o_Sel.EndKey(Unit: Word.WdUnits.wdLine, Extend: Type.Missing); o_Sel.HomeKey(Unit: Word.WdUnits.wdStory, Extend: Word.WdMovementType.wdExtend); o_Sel.Delete(); o_Sel.Delete(); } else { MessageBox.Show("Текст поиска не найден.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error); o_Word.Visible = true; return;; } } //int pars = o_Doc.Paragraphs.Count;// узнаем количество строк в документе int PagesCount = o_Word.Documents.Application.ActiveDocument.Content.ComputeStatistics(Word.WdStatistic.wdStatisticPages); for (int i = 1; i <= PagesCount; i++) { o_Sel.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, PagesCount, i); o_Sel.HomeKey(Unit: Word.WdUnits.wdLine, Extend: Type.Missing); o_Sel.MoveRight(Unit: Type.Missing, Count: 68, Extend: Word.WdMovementType.wdMove); o_Sel.ColumnSelectMode = true; o_Sel.MoveRight(Unit: Type.Missing, Count: 85, Extend: Word.WdMovementType.wdMove); o_Sel.MoveDown(Unit: Word.WdUnits.wdLine, Count: 109, Extend: Word.WdMovementType.wdMove); o_Sel.ColumnSelectMode = false; //MessageBox.Show("Остановка"); o_Sel.Delete(); //o_Sel.HomeKey(Unit: Word.WdUnits.wdLine, Extend: Type.Missing); //o_Sel.MoveDown(Unit: Word.WdUnits.wdLine, Count: 109, Extend: Type.Missing); } o_Doc.PageSetup.PageWidth = o_Word.CentimetersToPoints(21f); o_Doc.PageSetup.PageHeight = o_Word.CentimetersToPoints(29.7f); //o_Doc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4; o_Doc.PageSetup.LeftMargin = o_Word.CentimetersToPoints(2.35f); o_Word.ActivePrinter = n_PrintName; Object background = Type.Missing; Object append = Type.Missing; Object range = Type.Missing; Object outputFileName = Type.Missing; Object from = Type.Missing; Object to = Type.Missing; Object item = Type.Missing; Object copies = Type.Missing; Object pages = Type.Missing; Object pageType = Type.Missing; Object printToFile = Type.Missing; Object collate = Type.Missing; Object fileName = Type.Missing; Object activePrinterMacGX = Type.Missing; Object manualDuplexPrint = Type.Missing; Object printZoomColumn = Type.Missing; Object printZoomRow = Type.Missing; Object printZoomPaperWidth = Type.Missing; Object printZoomPaperHeight = Type.Missing; o_Doc.PrintOut(ref background, ref append, ref range, ref outputFileName, ref from, ref to, ref item, ref copies, ref pages, ref pageType, ref printToFile, ref collate, ref activePrinterMacGX, ref manualDuplexPrint, ref printZoomColumn, ref printZoomRow, ref printZoomPaperWidth, ref printZoomPaperHeight); o_Word.ActivePrinter = d_PrintName; Thread.Sleep(2000); Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; Object originalFormat = Type.Missing; Object routeDocument = Type.Missing; ((Word._Application)o_Word).Quit(ref saveChanges, ref originalFormat, ref routeDocument); o_Word = null; this.Cursor = Cursors.Default; tSSlb.Text = "Файл " + filename + " отправлен на " + n_PrintName; }
private void Format_otvet() { // Инициализация Word this.Cursor = Cursors.WaitCursor; Word.Application o_Word = new Word.Application(); Word.Document o_Doc = new Word.Document(); string d_PrintName = o_Word.ActivePrinter; string n_PrintName = comboBox1.SelectedItem.ToString(); if (chkVisible.Checked) { o_Word.Visible = false; } else { o_Word.Visible = true; } //Открытие и редактирование документа Object filename = txtFrom.Text + "\\otvet.txt"; Object confirmConversions = Type.Missing; Object readOnly = Type.Missing; Object addToRecentFiles = Type.Missing; Object passwordDocument = Type.Missing; Object passwordTemplate = Type.Missing; Object revert = Type.Missing; Object writePasswordDocument = Type.Missing; Object writePasswordTemplate = Type.Missing; Object format = Type.Missing; Object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingOEMCyrillicII; Object visible = Type.Missing; Object openConflictDocument = Type.Missing; Object openAndRepair = Type.Missing; Object documentDirection = Type.Missing; Object noEncodingDialog = Type.Missing; if (!System.IO.File.Exists(filename.ToString())) { o_Word.Visible = true; MessageBox.Show("Файл " + filename.ToString() + " не найден.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } o_Word.Documents.Open(ref filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair, ref documentDirection, ref noEncodingDialog); //Word.Document Doc = new Word.Document(); o_Doc = o_Word.Documents.Application.ActiveDocument; o_Doc.PageSetup.LeftMargin = o_Word.CentimetersToPoints(1.27f); o_Doc.PageSetup.RightMargin = o_Word.CentimetersToPoints(1.27f); o_Doc.PageSetup.TopMargin = o_Word.CentimetersToPoints(1.27f); o_Doc.PageSetup.BottomMargin = o_Word.CentimetersToPoints(1.27f); Word.Find o_Find = o_Word.Selection.Find; Word.Selection o_Sel = o_Word.Selection; o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); o_Find.Text = "ФИО: Переходченко М.В."; //Object wrap = Type.Missing; ; //Object replace = Type.Missing; ; o_Find.Execute(FindText: Type.Missing, MatchCase: false, MatchWholeWord: false, MatchWildcards: false, MatchSoundsLike: Type.Missing, MatchAllWordForms: false, Forward: true, Wrap: Type.Missing, Format: false, ReplaceWith: Type.Missing, Replace: Type.Missing); o_Sel.EndKey(Unit: Word.WdUnits.wdLine, Extend: Type.Missing); o_Sel.HomeKey(Unit: Word.WdUnits.wdStory, Extend: Word.WdMovementType.wdExtend); o_Sel.Delete(); o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); o_Find.Text = "----------------------------------------------------------------------------^p----------------------------------------------------------------------------"; o_Find.Replacement.Text = "----------------------------------------------------------------------------^m----------------------------------------------------------------------------"; Object wrap = Word.WdFindWrap.wdFindContinue; Object replace = Word.WdReplace.wdReplaceAll; o_Find.Execute(FindText: Type.Missing, MatchCase: false, MatchWholeWord: false, MatchWildcards: false, MatchSoundsLike: Type.Missing, MatchAllWordForms: false, Forward: true, Wrap: wrap, Format: false, ReplaceWith: Type.Missing, Replace: replace); o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); o_Word.ActivePrinter = n_PrintName; Object background = Type.Missing; Object append = Type.Missing; Object range = Type.Missing; Object outputFileName = Type.Missing; Object from = Type.Missing; Object to = Type.Missing; Object item = Type.Missing; Object copies = Type.Missing; Object pages = Type.Missing; Object pageType = Type.Missing; Object printToFile = Type.Missing; Object collate = Type.Missing; Object fileName = Type.Missing; Object activePrinterMacGX = Type.Missing; Object manualDuplexPrint = Type.Missing; Object printZoomColumn = Type.Missing; Object printZoomRow = Type.Missing; Object printZoomPaperWidth = Type.Missing; Object printZoomPaperHeight = Type.Missing; o_Doc.PrintOut(ref background, ref append, ref range, ref outputFileName, ref from, ref to, ref item, ref copies, ref pages, ref pageType, ref printToFile, ref collate, ref activePrinterMacGX, ref manualDuplexPrint, ref printZoomColumn, ref printZoomRow, ref printZoomPaperWidth, ref printZoomPaperHeight); int PagesCount = o_Word.Documents.Application.ActiveDocument.Content.ComputeStatistics(Word.WdStatistic.wdStatisticPages); o_Word.ActivePrinter = d_PrintName; Thread.Sleep(2000); Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; Object originalFormat = Type.Missing; Object routeDocument = Type.Missing; ((Word._Application)o_Word).Quit(ref saveChanges, ref originalFormat, ref routeDocument); o_Word = null; this.Cursor = Cursors.Default; tSSlb.Text = "Файл " + filename + " отправлен на " + n_PrintName + " Всего страниц: " + PagesCount.ToString(); }
public void Format_vedLic() { // Инициализация Word this.Cursor = Cursors.WaitCursor; Word.Application o_Word = new Word.Application(); Word.Document o_Doc = new Word.Document(); if (chkVisible.Checked) { o_Word.Visible = false; } else { o_Word.Visible = true; } string dt_pick = dTPicker.Value.ToString("dd.MM.yyyy"); string dt_pick_save = dTPicker.Value.ToString("MMdd"); string[] Filelst = System.IO.Directory.GetFiles(txtFrom.Text, "day*.txt"); if (System.IO.File.Exists(Filelst.ToString())) { o_Word.Visible = true; MessageBox.Show("Файл " + Filelst.ToString() + " не найден.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Открытие и редактирование документа //Object o_filename = txtFrom.Text + "\\DAY"+dt_pick_save+".TXT"; Object o_filename = Filelst[0]; Object confirmConversions = Type.Missing; Object readOnly = Type.Missing; Object addToRecentFiles = Type.Missing; Object passwordDocument = Type.Missing; Object passwordTemplate = Type.Missing; Object revert = Type.Missing; Object writePasswordDocument = Type.Missing; Object writePasswordTemplate = Type.Missing; Object format = Type.Missing; Object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingOEMCyrillicII; Object visible = Type.Missing; Object openConflictDocument = Type.Missing; Object openAndRepair = Type.Missing; Object documentDirection = Type.Missing; Object noEncodingDialog = Type.Missing; o_Word.Documents.Open(ref o_filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair, ref documentDirection, ref noEncodingDialog); //Редактирование документа o_Doc = o_Word.Documents.Application.ActiveDocument; o_Doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; o_Doc.PageSetup.LeftMargin = o_Word.CentimetersToPoints(1f); o_Doc.PageSetup.RightMargin = o_Word.CentimetersToPoints(1f); o_Doc.PageSetup.TopMargin = o_Word.CentimetersToPoints(1f); o_Doc.PageSetup.BottomMargin = o_Word.CentimetersToPoints(1f); Word.Find o_Find = o_Word.Selection.Find; Word.Selection o_Sel = o_Word.Selection; o_Sel.WholeStory(); //выделяет весь текст o_Sel.Font.Size = 4; o_Sel.HomeKey(Unit: Word.WdUnits.wdStory, Extend: Word.WdMovementType.wdMove); //o_Sel.TypeText("Ведомость открытых лицевых счетов "+dt_pick+" г."+"\r\r"); o_Sel.TypeText("Ведомость открытых лицевых счетов " + dt_pick + " г."); o_Sel.TypeParagraph(); o_Sel.TypeParagraph(); o_Sel.EndKey(Unit: Word.WdUnits.wdLine, Extend: Word.WdMovementType.wdExtend); o_Sel.Delete(); //Поиск и замена o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); o_Find.Text = "^m"; o_Find.Replacement.Text = " "; Object wrap = Word.WdFindWrap.wdFindContinue; Object replace = Word.WdReplace.wdReplaceAll; o_Find.Execute(FindText: Type.Missing, MatchCase: false, MatchWholeWord: false, MatchWildcards: false, MatchSoundsLike: Type.Missing, MatchAllWordForms: false, Forward: true, Wrap: wrap, Format: false, ReplaceWith: Type.Missing, Replace: replace); o_Find.ClearFormatting(); o_Find.Replacement.ClearFormatting(); o_Sel.EndKey(Unit: Word.WdUnits.wdStory, Extend: Type.Missing); o_Sel.TypeParagraph(); o_Sel.TypeParagraph(); o_Sel.TypeText("Руководитель ________________ Алпеева Галина Ивановна"); o_Sel.TypeParagraph(); o_Sel.TypeParagraph(); o_Sel.TypeText("Гл.бухгалтер ________________ Зотова Елена Сергеевна"); //Сохранение в формате .doc Object s_fileName = txtFrom.Text + "\\arch\\DAY" + dt_pick_save + ".doc"; Object fileformat = Word.WdSaveFormat.wdFormatDocument; o_Doc.SaveAs(ref s_fileName, ref fileformat); //Выход из Word без сохранения Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; Object originalFormat = Type.Missing; Object routeDocument = Type.Missing; ((Word._Application)o_Word).Quit(ref saveChanges, ref originalFormat, ref routeDocument); o_Word = null; this.Cursor = Cursors.Default; tSSlb.Text = "Файл " + o_filename + " сохранен в " + s_fileName; }
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); } }
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; // 替换原段落的内容 } } }