簡単なアプローチは、段落を使用してRange
オブジェクトを処理し、新しい段落を 1 つずつ挿入するだけです。
API ドキュメントを見ると、次のメソッドをParagraphs
実装していることがわかります。Add
ドキュメントに追加された新しい空白の段落を表す Paragraph オブジェクトを返します。(...) Range が指定されていない場合、新しい段落は選択範囲または範囲の後、またはドキュメントの最後に追加されます。
ソース: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.paragraphs.add(v=office.14).aspx
このようにして、ドキュメントに新しいコンテンツを追加することが簡単になります。
完全を期すために、ソリューションがどのように機能するかを示すサンプルを含めました。サンプルはループをfor
ループし、反復ごとに次を挿入します。
このサンプルは、次を使用して C# コンソール アプリケーションとして実装されています。
- .NET 4.5
- Microsoft Office Object Library バージョン 15.0、および
- Microsoft Word オブジェクト ライブラリ バージョン 15.0
... つまり、MS Office 2013 に同梱されている MS Word Interop API です。
using System;
using System.IO;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
namespace StackOverflowWordInterop
{
class Program
{
static void Main()
{
// Open word and a docx file
var wordApplication = new Application() { Visible = true };
var document = wordApplication.Documents.Open(@"C:\Users\myUserName\Documents\document.docx", Visible: true);
// "10" is chosen by random - select a value that fits your purpose
for (var i = 0; i < 10; i++)
{
// Insert text
var pText = document.Paragraphs.Add();
pText.Format.SpaceAfter = 10f;
pText.Range.Text = String.Format("This is line #{0}", i);
pText.Range.InsertParagraphAfter();
// Insert table
var pTable = document.Paragraphs.Add();
pTable.Format.SpaceAfter = 10f;
var table = document.Tables.Add(pTable.Range, 2, 3, WdDefaultTableBehavior.wdWord9TableBehavior);
for (var r = 1; r <= table.Rows.Count; r++)
for (var c = 1; c <= table.Columns.Count; c++)
table.Cell(r, c).Range.Text = String.Format("This is cell {0} in table #{1}", String.Format("({0},{1})", r,c) , i);
// Insert picture
var pPicture = document.Paragraphs.Add();
pPicture.Format.SpaceAfter = 10f;
document.InlineShapes.AddPicture(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "img_1.png"), Range: pPicture.Range);
}
// Some console ascii-UI
Console.WriteLine("Press any key to save document and close word..");
Console.ReadLine();
// Save settings
document.Save();
// Close word
wordApplication.Quit();
}
}
}