private void simpleButton3_Click(object sender, EventArgs e) { if (num < marklist.Count - 1) { num = num + 1;; if (W_Bkm.Exists(marklist[num].UID.ToString())) { object tempid = marklist[num].UID.ToString(); tempmk = W_Bkm.get_Item(ref tempid); tempmk.Select(); oldstr = tempmk.Range.Text; txtold.Text = oldstr; if (mkd.FindStr(marklist[num].UID.ToString()) != -1) { txtnew.Text = mkd.Data[mkd.FindStr(marklist[num].UID.ToString())].BQdata.ToString(); newstr = txtnew.Text; txtnew.SelectAll(); } else { newstr = "暂无值"; txtnew.Text = newstr; txtnew.SelectAll(); } } gridRefreh(); } else { MessageBox.Show("已到书签结尾!"); } }
private void Frm_FindandChangeTL_Load(object sender, EventArgs e) { adddata(); string conn = "LayoutID='" + layoutID + "' order by StartP"; IList <LayoutBookMark> templist = Common.Services.BaseService.GetList <LayoutBookMark>("SelectLayoutBookMarkList", conn); marklist = templist; if (marklist.Count == 0) { MessageBox.Show("无书签!请先添加书签"); this.Close(); return; } if (W_Bkm.Exists(marklist[0].UID.ToString())) { object tempid = marklist[0].UID.ToString(); tempmk = W_Bkm.get_Item(ref tempid); tempmk.Select(); oldstr = tempmk.Range.Text; txtold.Text = oldstr; if (mkd.FindStr(marklist[0].UID.ToString()) != -1) { txtnew.Text = mkd.Data[mkd.FindStr(marklist[0].UID.ToString())].BQdata.ToString(); data_object = mkd.Data[mkd.FindStr(marklist[0].UID.ToString())].BQdata; newstr = txtnew.Text; } else { newstr = "暂无值"; txtnew.Text = newstr; txtnew.SelectAll(); } } }
private void FillGuard3(GuardManager manager) { Word.Bookmark bookmark = word.FindBookMark(guard3); Guard guard = manager.GetGuard3(); bookmark.Select(); FillGuard(guard, bookmark.Range); }
//在书签位置插入一段文字,text为文字内容 public void InsertText(string bookmark, string text) { object oStart = bookmark; object range = wordDoc.Bookmarks.get_Item(ref oStart).Range; Microsoft.Office.Interop.Word.Bookmarks wdBookmarks = wordDoc.Bookmarks; Microsoft.Office.Interop.Word.Bookmark wdBookmark = wdBookmarks.get_Item(ref oStart); Microsoft.Office.Interop.Word.Selection wdSel; wdBookmark.Select(); wdSel = wordApp.Selection; wdSel.Text = text; wordDoc.Paragraphs.Last.Range.Text = "\n"; }
/// <summary> /// 光标移动到指定书签位置,书签不存在时不移动 /// </summary> /// <param name="bookMarkName"></param> /// <returns></returns> public bool GoToBookMark(string bookMarkName) { //是否存在书签 if (_wordDocument.Bookmarks.Exists(bookMarkName)) { //object what = Word.WdGoToItem.wdGoToBookmark; //object name = bookMarkName; //GoTo(what, _missing, _missing, name); Word.Bookmark bookmark = _wordDocument.Bookmarks.get_Item(bookMarkName); bookmark.Select(); return(true); } return(false); }
private void FillWeekDay() { DateTime firstDay = new DateTime(curentDate.Year, curentDate.Month, 1); DateTime last = firstDay.AddMonths(1).AddDays(-1); DateTime currentDay = firstDay; Word.Bookmark bookmark = word.FindBookMark(weekDay); bookmark.Select(); Word.Range wordRange = bookmark.Range; // диапазон документа Word wordRange.Text = currentDay.ToString("ddd"); for (currentDay = currentDay.AddDays(1); currentDay <= last; currentDay = currentDay.AddDays(1)) { wordRange = word.MoveCellRight(); wordRange.Text = currentDay.ToString("ddd"); } }
private void simpleButton1_Click(object sender, EventArgs e) { object tempid = tempmk.Name.ToString(); Word.Range tmpRng = tempmk.Range; tmpRng.Text = txtnew.Text; object oRng = tmpRng; W_Bkm.Add(tempid.ToString(), ref oRng); if (txtold.Text == txtnew.Text) { tmpRng.Font.Color = Word.WdColor.wdColorGold; } if (num >= marklist.Count - 1) { MessageBox.Show("标签已到结尾"); return; } num = num + 1;; if (W_Bkm.Exists(marklist[num].UID.ToString())) { object tempid2 = marklist[num].UID.ToString(); tempmk = W_Bkm.get_Item(ref tempid2); tempmk.Select(); oldstr = tempmk.Range.Text; txtold.Text = oldstr; if (mkd.FindStr(marklist[num].UID.ToString()) != -1) { txtnew.Text = mkd.Data[mkd.FindStr(marklist[num].UID.ToString())].BQdata.ToString(); newstr = txtnew.Text; txtnew.SelectAll(); } else { newstr = "暂无值"; txtnew.Text = newstr; txtnew.SelectAll(); } } gridRefreh(); }
//Used for non-modal dialogs to bring active document back up between text insertion public void Continue() { //Exit quietly if buffer hasn't started if (!started) { return; } //Verify hidden document is active if ((HiddenDoc as Word.Document) != Globals.ThisAddIn.Application.ActiveDocument) { HiddenDoc.Activate(); } //Hidden doc selection curSel = Globals.ThisAddIn.Application.Selection; //Hidden doc range Word.Range bufDocRange; //Select entire doc, save range curSel.WholeStory(); bufDocRange = curSel.Range; //Find end, put a bookmark there bufDocRange.SetRange(curSel.End, curSel.End); object bookmarkObj = bufDocRange; //Generate "Continue" hidden bookmark Word.Bookmark mark = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks.Add("Continue", ref bookmarkObj); mark.Select(); //Tell hidden document it has been saved to remove rare prompt to save document HiddenDoc.Saved = true; //Make primary document active Globals.ThisAddIn.Application.ActiveDocument.Activate(); }
public void Start() { try { //Make hidden document active to receive selection HiddenDoc.Activate(); //results in a slight application focus loss } catch (System.Runtime.InteropServices.COMException ex) { if (ex.Message == "Object has been deleted.") { //Open Blank document, then attach styles *and update HiddenDoc = Globals.ThisAddIn.Application.Documents.Add(ref missing, ref missing, ref missing, ref FalseObj); HiddenDoc.set_AttachedTemplate(ref templateObj); HiddenDoc.UpdateStyles(); HiddenDoc.Activate(); } else { throw; } } //Remove Continue Bookmark, if exists Word.Bookmarks hiddenDocBookmarks = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks; if (hiddenDocBookmarks.Exists("Continue")) { object deleteMarkObj = "Continue"; Word.Bookmark deleteMark = hiddenDocBookmarks.get_Item(ref deleteMarkObj); deleteMark.Select(); deleteMark.Delete(); } //Tell hidden document it has been saved to remove rare prompt to save document HiddenDoc.Saved = true; //Keep track when started started = true; }
private void print(List <Entity.Sample> samplelist, int num) { try { RemoveFiles(Server.MapPath(".") + "\\temp\\"); } catch (Exception e) { Log.log alog = new Log.log(); alog.Log(e.Message.ToString() + DateTime.Now.ToString()); } try { Random rd = new Random(); int oid = rd.Next(10000); Word.Application app = new Word.Application(); Word.Document doc = new Document(); object missing = System.Reflection.Missing.Value; object IsSave = true; try { string DocPath = Server.MapPath("../"); string TemplateFile = ""; TemplateFile = DocPath + "Sample\\template\\SampleTemplate0.doc"; //生成的具有模板样式的新文件 string FileName = DocPath + "Sample\\temp\\" + oid.ToString() + ".doc"; File.Copy(TemplateFile, FileName); object Obj_FileName = FileName; object Visible = false; object ReadOnly = false; //打开文件 doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing); doc.Activate(); for (int j = 1; j <= samplelist.Count; j++) { //找书签,赋值 // //样品编号 object name = "SampleID1_" + (j + num); if (doc.Bookmarks.Exists(name.ToString()) == true) { Word.Bookmark itemstr = doc.Bookmarks.get_Item(ref name); itemstr.Select(); itemstr.Range.Text = samplelist[j - 1].SampleID; } //项目名称 object ProjectName = "ProjectName1_" + (j + num); if (doc.Bookmarks.Exists(ProjectName.ToString()) == true) { Word.Bookmark itemstr = doc.Bookmarks.get_Item(ref ProjectName); itemstr.Select(); itemstr.Range.Text = samplelist[j - 1].projectname; // break; } //样品性状 object Profile = "Profile1_" + (j + num); if (doc.Bookmarks.Exists(Profile.ToString()) == true) { Word.Bookmark itemstr = doc.Bookmarks.get_Item(ref Profile); itemstr.Select(); itemstr.Range.Text = samplelist[j - 1].SampleProperty; //break; } //样品性状 object SampleDate = "SampleDate1_" + (j + num); if (doc.Bookmarks.Exists(SampleDate.ToString()) == true) { Word.Bookmark itemstr = doc.Bookmarks.get_Item(ref SampleDate); itemstr.Select(); itemstr.Range.Text = samplelist[j - 1].SampleDate.ToString("yyyy-MM-dd"); // break; } //for (int p = 1; p <= samplelist[j - 1].SampleItemList.Count; p++) //{ object Item = "ListItem" + (j + num); if (doc.Bookmarks.Exists(Item.ToString()) == true) { Word.Bookmark itemstr = doc.Bookmarks.get_Item(ref Item); itemstr.Select(); object type = @"Excel.Sheet.8"; object filename = @DocPath + "Sample\\template\\SampleTemplate.xls";; // TemplateFile = DocPath + "Sample\\template\\SampleTemplate.doc"; //app.Selection.InlineShapes[1].OLEFormat.Edit(); //object Mychart = app.Selection.InlineShapes[1].OLEFormat.Object; object range = itemstr.Range; app.Selection.InlineShapes.AddOLEObject(ref type, ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref range); } //} } doc.Save(); doc.Close(ref IsSave, ref missing, ref missing); app.Application.Quit(ref missing, ref missing, ref missing); ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "window.open('temp/" + oid.ToString() + ".doc','theNewWindow',' left=0,top=0,width='+ (screen.availWidth - 10) +',height='+ (screen.availHeight-50) +',scrollbars,resizable=yes,toolbar=no')", true); } catch (Exception mes) { doc.Close(ref IsSave, ref missing, ref missing); app.Application.Quit(ref missing, ref missing, ref missing); Log.log alog = new Log.log(); alog.Log(mes.Message.ToString() + DateTime.Now.ToString()); } } catch (Exception mes) { Log.log alog = new Log.log(); alog.Log(mes.Message.ToString() + DateTime.Now.ToString()); } }
/// <summary> /// 日記寫入Word /// </summary> public static void Export() { string tocDotx = Path.Combine(basePath, @"src\大事紀要_template_toc.dotx"); string contentDotx = Path.Combine(basePath, @"src\大事紀要_template_content.dotx"); //檢查目錄是否存在 if (!Directory.Exists(Path.Combine(basePath, "output"))) { //當目錄不存在時創建一個 Directory.CreateDirectory(Path.Combine(basePath, "output")); } string savePath = Path.Combine(basePath, @"output\output.docx"); Word.Application _app = new Word.Application(); _app.Visible = true; //顯示腳本作業 //_app.Visible = false; //背景腳本作業 Word.Document _doc = _app.Documents.Add(tocDotx); Word.Document _doc2 = _app.Documents.Add(contentDotx); // 取代年份 Word.Bookmark bookmark = _doc.Bookmarks["TocTitleYear"]; bookmark.Select(); Word.Find findObj = _app.Selection.Find; findObj.Text = "000"; findObj.Replacement.Text = "108"; findObj.Execute(Replace: Word.WdReplace.wdReplaceAll); _app.Selection.GoTo(Word.WdGoToItem.wdGoToPercent, Word.WdGoToDirection.wdGoToLast); _app.Selection.Select(); // 插入表格 _doc2.Activate(); var table = _doc2.Tables.Add(_app.Selection.Range, 3, 4); // 設定表格框線 table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; table.Borders.InsideLineWidth = Word.WdLineWidth.wdLineWidth050pt; table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; table.Borders.OutsideLineWidth = Word.WdLineWidth.wdLineWidth150pt; // 表格置中對齊 table.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter; table.Title = "○○○○室"; // 設定第一欄 月 0.92公分 table.Columns[1].SetWidth(_app.CentimetersToPoints(0.92f), Word.WdRulerStyle.wdAdjustSameWidth); // 設定第二欄 日 0.92公分 table.Columns[2].SetWidth(_app.CentimetersToPoints(0.92f), Word.WdRulerStyle.wdAdjustSameWidth); // 設定第三欄 項目 3.21公分 table.Columns[3].SetWidth(_app.CentimetersToPoints(3.21f), Word.WdRulerStyle.wdAdjustSameWidth); // 設定第四欄 內容 10.82公分 table.Columns[4].SetWidth(_app.CentimetersToPoints(10.82f), Word.WdRulerStyle.wdAdjustSameWidth); // 寫入表格標題、合併首行儲存格 string title = "○○○○室"; string yearTitle = "000年大事紀要"; table.Rows[1].HeightRule = Word.WdRowHeightRule.wdRowHeightExactly; table.Rows[1].Height = _app.CentimetersToPoints(1.3f); table.Cell(1, 1).Merge(table.Cell(1, 4)); table.Cell(1, 1).Range.set_Style("日記上冊標題"); table.Cell(1, 1).Range.Text = title + yearTitle; // 寫入表格欄位描述 // 設定欄位描述固定高度 0.8公分 table.Rows[2].HeightRule = Word.WdRowHeightRule.wdRowHeightExactly; table.Rows[2].Height = _app.CentimetersToPoints(0.8f); table.Rows[2].Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; table.Rows[2].Select(); _app.Selection.set_Style("日記欄位"); table.Cell(2, 1).Range.Text = "月"; table.Cell(2, 2).Range.Text = "日"; table.Cell(2, 3).Range.Text = "項目"; table.Cell(2, 4).Range.Text = "內容"; table.Cell(3, 1).Range.set_Style("日記日期"); table.Cell(3, 2).Range.set_Style("日記日期"); table.Cell(3, 3).Range.set_Style("日記內文"); table.Cell(3, 4).Range.set_Style("日記內文"); // 寫入內文 #region 物件測試資料 var data = GetData(); List <CoverDetial> coverStash = new List <CoverDetial>(); int iRow = 3; int startPage = _app.Selection.Range.get_Information(Word.WdInformation.wdActiveEndPageNumber); int endPage; data.ForEach(e => { Console.WriteLine($"寫入:{e.Month}月{e.Day}日 {e.Event}, 機敏:{e.IsBold}"); //log if (iRow > 3) { table.Rows.Add(); //寫入第一行內文之後才要換行 } // 寫入資料列 table.Cell(iRow, 1).Range.Text = e.Month; table.Cell(iRow, 2).Range.Text = e.Day; table.Cell(iRow, 3).Range.Text = e.Event; table.Cell(iRow, 4).Range.Text = e.Content; // 判斷有無機敏(目前是整列粗體顯示) if (e.IsBold) { table.Rows[iRow].Select(); _app.Selection.Font.Bold = 1; // 加入機敏期限暫存列 coverStash.Add(e.CoverDetial); } else { table.Rows[iRow].Select(); _app.Selection.Font.Bold = 0; } // 判斷有無跨頁,當跨頁時判斷插入機敏文字方塊 endPage = _app.Selection.Range.get_Information(Word.WdInformation.wdActiveEndPageNumber); if (startPage < endPage || data.IndexOf(e) == data.Count - 1) { // 若前頁有機敏文字 if (coverStash.Any()) { coverStash = coverStash.OrderByDescending(x => x.Year) .ThenByDescending(x => x.Month) .ThenByDescending(x => x.Day) .ToList(); CoverDetial cover = coverStash.First(); // 游標移到指定頁 _app.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, startPage); // 插入文字方塊 float left = _app.CentimetersToPoints(-0.25f) // 距左邊界-0.25公分 , top = _app.CentimetersToPoints(-1f) // 距上邊界-1公分 , width = _app.CentimetersToPoints(16.28f) // 寬16.28公分 , height = 20; // 高2倍字元(pt) Word.Shape textBox = _doc2.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, left, top, width, height, _app.Selection.Range); textBox.Line.Visible = Office.MsoTriState.msoFalse; // 取消顯示外框 textBox.WrapFormat.Type = Word.WdWrapType.wdWrapFront; // 文字在後 textBox.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionLeftMarginArea; textBox.Left = left; textBox.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionTopMarginArea; textBox.Top = top; textBox.TextFrame.TextRange.set_Style("頁首"); textBox.TextFrame.TextRange.Text = $"機密(本件屬國家機密亦屬軍事機密,保密至民國{cover.Year}年{cover.Month}月{cover.Day}日解除密等)"; // 清空機敏期限暫存列 coverStash.Clear(); // 如果跨頁的列是機敏,再將它加入暫存列 if (e.IsBold) { coverStash.Add(e.CoverDetial); } // 回到表格最後一列 table.Rows[iRow].Select(); } } // 迭代至下一列 iRow++; // 紀錄下一列操作之前的起始頁 startPage = _app.Selection.Range.get_Information(Word.WdInformation.wdActiveEndPageNumber); }); #endregion // 全選 _app.ActiveDocument.Range(_app.ActiveDocument.Content.Start, _app.ActiveDocument.Content.End).Select(); // 複製 _app.Selection.Copy(); // 回到有目錄的範本 _doc.Activate(); // 貼上 _app.Selection.Paste(); // 將最後一行的段落設定為固定行高 1點,以避免產生空白頁 //_app.Selection.GoTo(Word.WdGoToItem.wdGoToPercent, Word.WdGoToDirection.wdGoToLast); //_app.Selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly; //_app.Selection.ParagraphFormat.LineSpacing = 1.0f; _doc.TablesOfFigures[1].Update(); //_doc.TablesOfFigures[2].Update(); _doc.SaveAs2(savePath); _doc2.Close(false); //_doc.Close(false); //_app.Quit(false); }
private void simpleButton3_Click(object sender, EventArgs e) { if (num < marklist.Count - 1) { num = num + 1; ; if (W_Bkm.Exists(marklist[num].UID.ToString())) { object tempid = marklist[num].UID.ToString(); tempmk = W_Bkm.get_Item(ref tempid); tempmk.Select(); oldstr = tempmk.Range.Text; txtold.Text = oldstr; if (mkd.FindStr(marklist[num].UID.ToString()) != -1) { txtnew.Text = mkd.Data[mkd.FindStr(marklist[num].UID.ToString())].BQdata.ToString(); newstr = txtnew.Text; txtnew.SelectAll(); } else { newstr = "����ֵ"; txtnew.Text = newstr; txtnew.SelectAll(); } } gridRefreh(); } else { MessageBox.Show("�ѵ���ǩ��β��"); } }
private void simpleButton1_Click(object sender, EventArgs e) { object tempid = tempmk.Name.ToString(); Word.Range tmpRng = tempmk.Range; tmpRng.Text = txtnew.Text; object oRng = tmpRng; W_Bkm.Add(tempid.ToString(), ref oRng); if (txtold.Text == txtnew.Text) { tmpRng.Font.Color = Word.WdColor.wdColorGold; } if (num >= marklist.Count - 1) { MessageBox.Show("��ǩ�ѵ���β"); return; } num = num + 1; ; if (W_Bkm.Exists(marklist[num].UID.ToString())) { object tempid2 = marklist[num].UID.ToString(); tempmk = W_Bkm.get_Item(ref tempid2); tempmk.Select(); oldstr = tempmk.Range.Text; txtold.Text = oldstr; if (mkd.FindStr(marklist[num].UID.ToString()) != -1) { txtnew.Text = mkd.Data[mkd.FindStr(marklist[num].UID.ToString())].BQdata.ToString(); newstr = txtnew.Text; txtnew.SelectAll(); } else { newstr = "����ֵ"; txtnew.Text = newstr; txtnew.SelectAll(); } } gridRefreh(); }
private void Frm_FindandChangeTL_Load(object sender, EventArgs e) { adddata(); string conn = "LayoutID='" + layoutID + "' order by StartP"; IList<LayoutBookMark> templist = Common.Services.BaseService.GetList<LayoutBookMark>("SelectLayoutBookMarkList", conn); marklist = templist; if (marklist.Count==0) { MessageBox.Show("����ǩ�����������ǩ"); this.Close(); return; } if (W_Bkm.Exists(marklist[0].UID.ToString())) { object tempid = marklist[0].UID.ToString(); tempmk = W_Bkm.get_Item(ref tempid); tempmk.Select(); oldstr = tempmk.Range.Text; txtold.Text = oldstr; if (mkd.FindStr(marklist[0].UID.ToString())!=-1) { txtnew.Text = mkd.Data[mkd.FindStr(marklist[0].UID.ToString())].BQdata.ToString(); data_object = mkd.Data[mkd.FindStr(marklist[0].UID.ToString())].BQdata; newstr = txtnew.Text; } else { newstr = "����ֵ"; txtnew.Text = newstr; txtnew.SelectAll(); } } }