Quantcast
Channel: Word for Developers forum
Viewing all articles
Browse latest Browse all 4350

Export data from C# winForms treeView to MS-Word MergeFields

$
0
0

I have a winForm app written in c# and i have a treeview contains files from a directory. It also contains data about each files (fullpath,creation time,size) it look like this:

  • http://i.stack.imgur.com/MZOaW.png

This is the temlate:

  • http://i.stack.imgur.com/1erPq.png

I am trying to export this data to MS-Word template like the above 

My problem is to duplicate the mergeFields for each File and to insert each file properties (The number of files changes) in place to look like this:

  • http://i.stack.imgur.com/hDJOh.png

This is my Code:

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);
    }

I'm tying to look for an answer for a long time with no success.
I hope anyone here could help me.


Viewing all articles
Browse latest Browse all 4350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>