private void OpenDocument(word.Application application) { word.Selection selection = application.Selection; if (selection == null) { MessageBox.Show(@"Selection is null"); return; } switch (selection.Type) { case word.WdSelectionType.wdSelectionNormal: case word.WdSelectionType.wdSelectionIP: { var range = _document.Range(); range.Select(); richTextBox_Document.Text = range.Text; TextParseManager.FullWordText = range.Text; toolStripStatusLabel_Главы.Text = @"Главы: " + WordHelpers.FindChapters(_document); var array = WordHelpers.SplitByChapters(TextParseManager.FullWordText); break; } default: MessageBox.Show(@"Неподдерживаемый тип Selection"); break; } _document.RemoveDocumentInformation(word.WdRemoveDocInfoType.wdRDIAll); application.Documents.Save(NoPrompt: true, OriginalFormat: true); }
public void WriteToWord(string text) { Word.Application ap = new Word.Application(); try { Word.Document doc = ap.Documents.Open(@"C:\Users\zao.CCD\Desktop\3Course-Program-Lab-5\Log.doc", ReadOnly: false, Visible: false); doc.Activate(); Word.Selection sel = ap.Selection; if (sel != null) { switch (sel.Type) { case Word.WdSelectionType.wdSelectionIP: sel.TypeText(DateTime.Now.ToString()); sel.TypeParagraph(); sel.TypeText(text); sel.TypeParagraph(); break; default: Console.WriteLine("Selection type not handled; no writing done"); break; } // Remove all meta data. doc.RemoveDocumentInformation(Word.WdRemoveDocInfoType.wdRDIAll); ap.Documents.Save(NoPrompt: true, OriginalFormat: true); } else { Console.WriteLine("Unable to acquire Selection...no writing to document done.."); } ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false); } catch (Exception ex) { Console.WriteLine("Exception Caught: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?) } finally { ((Word._Application)ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false); System.Runtime.InteropServices.Marshal.ReleaseComObject(ap); } }
//public static void WriteCreate(Order order, string employee,string path) //{ // var app = new Word.Application(); // app.Visible = false; // var doc = app.Documents.Add(); // var r = doc.Range(); // r.Text =order.Text+" "+employee; // doc.SaveAs2($@"C:\1\{employee}{order.Id}.doc"); //} public static void Write(Order order, string employee, string path) { Word.Application ap = new Word.Application(); try { using (File.Create(path + $"{employee}{order.Id}.doc")) { } Word.Document doc = ap.Documents.Open(path + $"{employee}{order.Id}.doc", ReadOnly: false, Visible: false); doc.Activate(); Word.Selection sel = ap.Selection; if (sel != null) { switch (sel.Type) { case Word.WdSelectionType.wdSelectionIP: sel.TypeText("Дата: " + order.Date.ToString()); sel.TypeParagraph(); sel.TypeText("От кого: " + order.From.Name); sel.TypeParagraph(); sel.TypeText("Кому: " + order.To.Name); sel.TypeParagraph(); sel.TypeText("Номер документа: " + order.Id.ToString()); sel.TypeParagraph(); sel.TypeText("Текщее состояние документу: " + order.Status.Name); sel.TypeParagraph(); sel.TypeText("Описание документа: " + order.Text); sel.TypeParagraph(); sel.TypeText("Инвентарь: "); sel.TypeParagraph(); foreach (var item in order.ItemTransactions) { sel.TypeText("Название: " + item.Name); sel.TypeParagraph(); sel.TypeText("Количество: " + item.Count.ToString()); sel.TypeParagraph(); } break; } // Remove all meta data. doc.RemoveDocumentInformation(Word.WdRemoveDocInfoType.wdRDIAll); ap.Documents.Save(NoPrompt: true, OriginalFormat: true); } else { // Console.WriteLine("Unable to acquire Selection...no writing to document done.."); } ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false); } catch (Exception ex) { // Console.WriteLine("Exception Caught: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?) } finally { ((Word._Application)ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false); System.Runtime.InteropServices.Marshal.ReleaseComObject(ap); } }