public static void ReplaceLabel(Word.Document doc, Word.Application app, string label, string replaceText, bool replaceAll) { string repl = (replaceText == null ? String.Empty : replaceText); doc.Select(); object findText = "$[" + label + "]"; object matchCase = false; object matchWholeWord = false; object matchWildCards = false; object matchSoundsLike = false; object matchAllForms = false; object forward = true; object wrap = Word.WdFindWrap.wdFindContinue; object format = false; object replaceWith = repl; object replace; if (replaceAll) { replace = Word.WdReplace.wdReplaceAll; } else { replace = Word.WdReplace.wdReplaceOne; } app.Selection.Find.ExecuteOld(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllForms, ref forward, ref wrap, ref format, ref replaceWith, ref replace); /*object line = Word.WdUnit.wdLine; * object oFalse = false; * app.Selection.EndKey(ref line, ref oFalse);*/ }
private void button1_Click(object sender, EventArgs e) { Word.Application app = new Word.Application(); Word.Document doc = app.Documents.Add(); doc.Select(); app.Selection.TypeParagraph(); app.Selection.TypeText(" ТАЛОН "); app.Selection.TypeParagraph(); app.Selection.TypeText("на прием к врачу : "); app.Selection.TypeParagraph(); app.Selection.TypeText("Имя пациента : " + ticket.patCard.name); app.Selection.TypeParagraph(); app.Selection.TypeText("Специализация : " + ticket.specialization.name); app.Selection.TypeParagraph(); app.Selection.TypeText("ФИО врача : " + ticket.doctor.name); app.Selection.TypeParagraph(); app.Selection.TypeText("Дата : " + ticket.date.ToString("dd.MM.yyyy")); app.Selection.TypeParagraph(); app.Selection.TypeText("Кабинет : " + ticket.cabinet.number); app.Selection.TypeParagraph(); app.Selection.TypeText("Время : " + ticket.time); app.PrintOut(); //app.Visible = true; this.Close(); }
/// <summary> /// 粘贴文档 /// </summary> /// <param name="WordTemplet">需要粘贴的文档</param> /// <param name="CopyDoc">拷贝的文档</param> public void PasteWord(Microsoft.Office.Interop.Word.Document WordTemplet, Microsoft.Office.Interop.Word.Document CopyDoc) { CopyDoc.Select(); WordApplic.Selection.Copy(); Microsoft.Office.Interop.Word.Paragraph Word_Para; if (WordTemplet.Paragraphs.Count == 0) //如果段落数等于0,则新增一个段落 { WordTemplet.Paragraphs.Add(ref missing); } Word_Para = WordTemplet.Paragraphs.Last; //定位到最后一个段落 if (WordTemplet.Paragraphs.Count > 1) //如果段落数大于1,就需要增加换页符 { object pagebreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage; Word_Para.Range.InsertBreak(ref pagebreak); //在最后一个段落后插入一个换页符 } Word_Para.Range.Paste(); //粘贴复制的模板 Word_Para = null; System.Windows.Forms.Clipboard.Clear(); //清空剪切板 CloseDoc(CopyDoc, false); //关闭被复制的文档 }
public void SetText(string strText, MSWord.WdParagraphAlignment hAlignment, FontParams font = null) { if (m_pSelection == null) { if (m_pDoc == null) { m_pDoc = m_pApp.ActiveDocument; } if (m_pApp == null) { return; } m_pDoc.Select(); m_pSelection = m_pApp.Selection; if (m_pSelection == null) { return; } } object varInfo = m_pSelection.get_Information(MSWord.WdInformation.wdWithInTable); if (Convert.ToBoolean(varInfo) == true) { m_pDoc.Select(); m_pSelection = m_pApp.Selection; if (m_pSelection == null) { return; } m_pSelection.Collapse(0); } MSWord.ParagraphFormat pf = m_pSelection.ParagraphFormat; pf.Alignment = hAlignment; m_pSelection.Text = strText; if (font != null) { PutFont(font, m_pSelection); } m_pSelection.Collapse(0); }
public static void addImage() { Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = @"C:\Users\Netlex\Google Drive\Minhas automatizações\Outros"; //openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; //selecao.Select(); doc.Select(); if (openFileDialog.ShowDialog() == DialogResult.OK) { doc.InlineShapes.AddPicture(openFileDialog.FileName); } } }
private bool ExportDataGridview(DataGridView dgv, bool isShowWord) { //实例化Word文档对象 Microsoft.Office.Interop.Word.Document mydoc = new Microsoft.Office.Interop.Word.Document(); Microsoft.Office.Interop.Word.Table mytable; //声明Word表格 Microsoft.Office.Interop.Word.Selection mysel; //声明Word选区 Object myobj; if (dgv.Rows.Count == 0) { return(false); } //建立Word对象 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); myobj = System.Reflection.Missing.Value; mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj); word.Visible = isShowWord; mydoc.Select(); mysel = word.Selection; //将数据生成Word表格文件 mytable = mydoc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj); //设置列宽 mytable.Columns.SetWidth(80, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone); for (int i = 0; i < dgv.ColumnCount; i++)//输出列标题数据 { mytable.Cell(1, i + 1).Range.InsertAfter(dgv.Columns[i].HeaderText); } for (int i = 0; i < dgv.RowCount - 1; i++)//输出控件中的记录 { for (int j = 0; j < dgv.ColumnCount; j++) { mytable.Cell(i + 2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString()); } } return(true); }
private void CopyBodyToMeeting(AppointmentItem ai, ref MeetingItem meetingItem) { ThisAddIn.g_log.Info("Copy AppointmentItem body to MeetingItem enter"); Word.Document Doc = new Word.Document(); Word.Document Doc2 = new Word.Document(); Word.Application App1; Word.Selection Sel; Doc = ai.GetInspector.WordEditor as Word.Document; if (Doc != null) { ThisAddIn.g_log.Info(string.Format("appointmentItem doc length is {0}.", Doc.Content.Text.Length)); Doc.Activate(); Doc.Select(); App1 = Doc.Windows.Application; Sel = App1.Selection; Sel.WholeStory(); Sel.Copy(); Doc2 = meetingItem.GetInspector.WordEditor as Word.Document; if (Doc2 != null) { Doc2.Activate(); object start = 0; Range newRang = Doc2.Range(ref start, ref start); int ioo = Doc2.Sections.Count; Section sec = Doc2.Sections[1]; sec.Range.InsertBreak(Type.Missing);//插入换行符 sec.Range.PasteAndFormat(WdRecoveryType.wdPasteDefault); meetingItem.Save(); ThisAddIn.g_log.Info(string.Format("meetingItem doc length is {0}.", Doc2.Content.Text.Length)); //ThisAddIn.g_log.Info(string.Format("mailItem body length is {0}.", meetingItem.Body.Length)); if (meetingItem.Body == null || meetingItem.Body.Length < 100) { Doc2.Activate(); ((_Inspector)(meetingItem.GetInspector)).Activate(); } } } Doc = null; Doc2 = null; App1 = null; Sel = null; ThisAddIn.g_log.Info("Copy AppointmentItem body to MeetingItem exit"); }
private void CopyBody(AppointmentItem ai, MailItem mailItem) { ThisAddIn.g_log.Info("Copy AppointmentItem body to MailItem enter"); Word.Document Doc = new Word.Document(); Word.Document Doc2 = new Word.Document(); Word.Application App1; Word.Selection Sel; Doc = ai.GetInspector.WordEditor as Word.Document; Word.Application App2; Word.Selection Sel2; if (Doc != null) { ThisAddIn.g_log.Info(string.Format("appointmentItem doc length is {0}.", Doc.Content.Text.Length)); Doc.Activate(); Doc.Select(); App1 = Doc.Windows.Application; Sel = App1.Selection; Sel.WholeStory(); Sel.Copy(); Doc2 = mailItem.GetInspector.WordEditor as Word.Document; if (Doc2 != null) { Doc2.Activate(); Doc2.Select(); App2 = Doc2.Windows.Application; Sel2 = App2.Selection; Sel2.WholeStory(); try { Sel2.PasteAndFormat(WdRecoveryType.wdPasteDefault); } catch (System.Exception ex) { ThisAddIn.g_log.Error(string.Format("copy to body exception, {0}", ex.ToString())); } mailItem.Save(); ThisAddIn.g_log.Info(string.Format("mailItem doc length is {0}.", Doc2.Content.Text.Length)); if (mailItem.Body == null || mailItem.Body.Length < 100) { Doc2.Activate(); ((_Inspector)(mailItem.GetInspector)).Activate(); } } } Doc = null; Doc2 = null; App1 = null; App2 = null; Sel = null; Sel2 = null; ThisAddIn.g_log.Info("Copy AppointmentItem body to MailItem exit"); }
private void CopyBodyToMeeting(AppointmentItem ai, ref MeetingItem meetingItem) { ThisAddIn.g_log.Info("Copy AppointmentItem body to MeetingItem enter"); Word.Document Doc = new Word.Document(); Word.Document Doc2 = new Word.Document(); //Word.Application App1; //Word.Selection Sel; Doc = ai.GetInspector.WordEditor as Word.Document; // Word.Application App2; // Word.Selection Sel2; if (Doc != null) { ThisAddIn.g_log.Info(string.Format("appointmentItem doc length is {0}.", Doc.Content.Text.Length)); Doc.Activate(); Doc.Select(); //App1 = Doc.Windows.Application; //Sel = App1.Selection; //Sel.WholeStory(); //Sel.Copy(); Doc.Range().WholeStory(); Doc.Range().Copy(); Doc2 = meetingItem.GetInspector.WordEditor as Word.Document; if (Doc2 != null) { Doc2.Activate(); object start = 0; Range newRang = Doc2.Range(ref start, ref start); int ioo = Doc2.Sections.Count; Section sec = Doc2.Sections[1]; sec.Range.InsertBreak(Type.Missing);//插入换行符 try { //sec.Range.PasteAndFormat(WdRecoveryType.wdPasteDefault); sec.Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting); meetingItem.Save(); ThisAddIn.g_log.Info(string.Format("meetingItem doc length is {0}.", Doc2.Content.Text.Length)); if (meetingItem.Body == null || meetingItem.Body.Length < 100) { Doc2.Activate(); ((_Inspector)(meetingItem.GetInspector)).Activate(); ThisAddIn.g_log.Info("meetingItems's body is null Or body length is short,Activate over"); } } catch (System.Exception ex) { ThisAddIn.g_log.Info("CopyBodyToMeeting comes Exception " + ex.ToString()); } } else { ThisAddIn.g_log.Info("Doc is null"); } } if (Doc != null) { Marshal.ReleaseComObject(Doc); Doc = null; } if (Doc2 != null) { Marshal.ReleaseComObject(Doc2); Doc2 = null; } //Doc = null; //Doc2 = null; ////App1 = null; ////Sel = null; //// App2 = null; //// Sel2 = null; ThisAddIn.g_log.Info("Copy AppointmentItem body to MeetingItem exit"); }