Справочник по C#

    Исходники по языку программирования CSharp

    Запись в файл Microsoft Word (2003,2007): Справочник по C#

    /
    /
    /
    55 Views


    //using Word = Microsoft.Office.Interop.Word;
    
    Word.Application ap = new Word.Application();
    try
    {
    
    Word.Document doc = ap.Documents.Open(@"D:MyWord.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("Microsoft Word");
    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);
    }


    Результат:

    Запись в файл Microsoft Word (2003,2007): Справочник по C#

    0.00 (0%) 0 votes

    moyadcode13
    • Facebook
    • Twitter
    • Google+
    • Linkedin
    • Pinterest
    moyadcode10
    moyadcode11
    moyadcode9