![]() |
#1
|
|||
|
|||
![]()
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 I am trying to export this data to MS-Word template look like this: http://i.stack.imgur.com/1erPq.png 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: 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 hope anyone here could help me. Last edited by macropod; 04-17-2014 at 04:26 AM. Reason: Added code tags & formatting |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Export VBA form Data to CSV | rhys.downard | Outlook | 0 | 02-29-2012 07:35 AM |
![]() |
virencm | Project | 4 | 02-08-2012 03:18 PM |
Export data from multiple attachments into a single excel document | Woolstar | Outlook | 0 | 12-07-2011 09:49 AM |
![]() |
Catalin.B | Excel Programming | 2 | 09-08-2011 11:37 PM |
Create a Custome Form and export data to Access | ashleybyrdnc | Office | 0 | 03-05-2010 09:41 AM |