1

C# で HTML コンテンツを含む Word 文書を作成したいと考えています。ただし、次のコードは、コンテンツを HTML 形式ではなくテキストとして表示します...

これが私のコードです:

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();  
              app.Visible=true;

              object template = Missing.Value;
              object newTemplate = Missing.Value;
              object documentType = Missing.Value;
              object visible = true;  //Show the doc while we work.
              _Document doc = app.Documents.Add(ref template,
                       ref newTemplate,
                       ref documentType,
                       ref visible);   
            doc.Words.First.InsertBefore ("<html><body>HTML content here!</body></html>");
4

1 に答える 1

3

HTML ファイルをソースとして使用できます。

using System;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;

class Program
{
    [STAThread]
    public static void Main()
    {
        var file = new FileInfo("input.html");
        Microsoft.Office.Interop.Word.Application app 
            = new Microsoft.Office.Interop.Word.Application();
        try
        {
            app.Visible = true;
            object missing = Missing.Value;
            object visible = true;
            _Document doc = app.Documents.Add(ref missing,
                     ref missing,
                     ref missing,
                     ref visible);
            var bookMark = doc.Words.First.Bookmarks.Add("entry");
            bookMark.Range.InsertFile(file.FullName);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            app.Quit();
        }
    }
}
于 2013-08-14T11:30:14.587 に答える