1

こんにちは、c# を使用して winforms アプリケーションから Word ドキュメントを自動化しています。ドキュメントには、コードから入力した多数の差し込み印刷フィールドが含まれています。これは、固定フィールドではうまく機能します。

ただし、ドキュメントには、実行時に n 回繰り返される繰り返しセクションがいくつか含まれています。複数回挿入できるある種のブロックを作成する方法を教えてもらえますか?

2 つのブックマークの間にテキストを挿入しようとしましたが、ページの書式が乱れます。

アドバイスをいただければ幸いです。

編集:

以下の関数は、データを単語テンプレートにマージするために使用するものです。Dictionary は、最初の文字列にフィールド名を保持し、2 番目の文字列に内容を保持します。

ユーザーには、ドキュメント内のすべてのフィールド コードを含むサンプル Word テンプレートが提供され、フィールドの順序とスタイルを自分で変更できます。

public static void CreateBody(Application wApp, Document wDoc, Dictionary<string, string> fieldContent)
    {
        //Loop through fields in document body.
        foreach (Field mergeField in wDoc.Fields)
        {
            //Find mailmerge fields in the Word document body.
            if (mergeField.Type == WdFieldType.wdFieldMergeField)
            {
                //Get the fieldName from the template file.
                string fieldText = mergeField.Code.Text;
                string fieldName = GetFieldName(fieldText);

                if (fieldContent.ContainsKey(fieldName))
                {
                    mergeField.Select();
                    if (fieldContent[fieldName] != "")
                    {
                        wApp.Selection.TypeText(fieldContent[fieldName]);
                    }
                    else
                    {
                        //If the field has no content remove the field text from the word file.
                        wApp.Selection.TypeText(" ");                            
                    }
                }

            }
        }

現在、テンプレート内の差し込み印刷フィールドのブロックを繰り返し、レコードごとに 1 回、複数回挿入する方法を探しています。

4

1 に答える 1