1

私はc#で書かれたwinFormアプリを持っていて、ディレクトリからのファイルを含むツリービューを持っています。また、次のような各ファイルに関するデータ (フルパス、作成時間、サイズ) も含まれています。

これは私のツリービューです

このデータを次のような MS-Word テンプレートにエクスポートしようとしています。 ここに画像の説明を入力

私の問題は、各ファイルのmergeFieldsを複製し、各ファイルのプロパティ(ファイル数の変更)を次のように挿入することです:

ここに画像の説明を入力

これは私のコードです:

private void btnExportWord_Click_1(object sender, EventArgs e)
    {
        object oMissing = Missing.Value;
        Word.Application oWord = new Word.Application();
        Word.Document oWordDoc = new Word.Document();
        oWord.Visible = false;
        oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);           
        Object oTemplatePath = @"C:\test\MyXMLTemplate.dotx";         
        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

        for (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)
        {
            string strFilename = treeViewXMLFiles.Nodes[0].Nodes[i].Text;
            string strFull_path = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
            string strCreationTime = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
            string strSize = treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text;

            foreach (Word.Field myMergeField in oWordDoc.Fields)
            {
                Word.Range rngFieldCode = myMergeField.Code;
                String fieldText = rngFieldCode.Text;
                if (fieldText.StartsWith(" MERGEFIELD"))
                {
                    Int32 endMerge = fieldText.IndexOf("\\");
                    Int32 fieldNameLength = fieldText.Length - endMerge;
                    String fieldName = fieldText.Substring(11, endMerge - 11);
                    fieldName = fieldName.Trim();

                    if (fieldName == "File_Name")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strFilename);
                    }
                    if (fieldName == "Full_Path")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strFull_path);
                    }
                    if (fieldName == "CreationTime")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strCreationTime);
                    }
                    if (fieldName == "Size")
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(strSize);
                    }
                }
            }                
        }           
        Object oSaveAsFile = (Object)@"C:\test\FINISHED_XML_Template.doc";            
        oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

        oWordDoc.Close(false, ref oMissing, ref oMissing);
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
    }

私は長い間答えを探していましたが、成功しませんでした。
ここの誰かが私を助けてくれることを願っています。

4

1 に答える 1