Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Mail Merge

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 04-17-2014, 03:42 AM
nisim651 nisim651 is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Apr 2014
Posts: 4
nisim651 is on a distinguished road
Default Export data from C# winForms treeView to MS-Word MergeFields

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'm tying to look for an answer for a long time with no success.
I hope anyone here could help me.

Last edited by macropod; 04-17-2014 at 04:26 AM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 04-17-2014, 04:58 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,249
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

If all you are outputting is just the "File_Name", "Full_Path", "CreationTime" and "Size", I have to wonder why you're bothering with mergefields. After all, you're not using them for a mailmerge - all you're doing is using them as placeholders that you replace with your data. You could just as easily insert them as strings into an ordinary document with code like:

oWord.Characters.Last.InsertParagraphAfter;
oWord.Characters.Last.InsertAfter strFilename;
oWord.Characters.Last.InsertParagraphAfter;
oWord.Characters.Last.InsertAfter strFull_path;
oWord.Characters.Last.InsertParagraphAfter;
oWord.Characters.Last.InsertAfter strCreationTime;
oWord.Characters.Last.InsertParagraphAfter;
oWord.Characters.Last.InsertAfter strSize;
oWord.Characters.Last.InsertParagraphAfter;
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 04-17-2014, 05:12 AM
nisim651 nisim651 is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Apr 2014
Posts: 4
nisim651 is on a distinguished road
Default

Can you show me how to insert this to my code. (how the new code should be look)?
Reply With Quote
  #4  
Old 04-17-2014, 05:24 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,249
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Basically, those lines would replace your entire foreach loop. I'm not a C# programmer, but it seems to me your code could be reduced to:
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++)
  {
    oWord.Characters.Last.InsertParagraphAfter;
    oWord.Characters.Last.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Text;
    oWord.Characters.Last.InsertParagraphAfter;
    oWord.Characters.Last.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
    oWord.Characters.Last.InsertParagraphAfter;
    oWord.Characters.Last.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
    oWord.Characters.Last.InsertParagraphAfter;
    oWord.Characters.Last.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text; 
    oWord.Characters.Last.InsertParagraphAfter;
  }
  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);
}
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 04-17-2014, 06:52 AM
nisim651 nisim651 is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Apr 2014
Posts: 4
nisim651 is on a distinguished road
Default

What should i do if i dont want this data at the end of the document but between 2 spesific paragraph?
Reply With Quote
  #6  
Old 04-17-2014, 03:10 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,249
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Do all of these records go in a list between the same pair of paragraphs, or are you repeating the paragraphs between the records?
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #7  
Old 04-17-2014, 07:44 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,249
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Cross-posted at: http://social.msdn.microsoft.com/For...?forum=worddev
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #8  
Old 04-18-2014, 02:25 AM
nisim651 nisim651 is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Apr 2014
Posts: 4
nisim651 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Do all of these records go in a list between the same pair of paragraphs, or are you repeating the paragraphs between the records?
I want to add these record in a list between the two paragraphs with no repeating of the paragraphs
Reply With Quote
  #9  
Old 04-18-2014, 04:17 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,249
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Again, bearing in mind I'm not a C# programmer, you might put a bookmark where you want the output to go, then use something like:
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;
  Object oTemplatePath = @"C:\test\MyXMLTemplate.dotx";
  oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
  Using oWordDoc.Bookmarks("BookmarkName").Range;
  {
    For (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)
    {
      .InsertParagraphAfter;
      .InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Text;
      .InsertParagraphAfter;
      .InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
      .InsertParagraphAfter;
      .InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
      .InsertParagraphAfter;
      .InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text; 
      .InsertParagraphAfter;
    }
  }
  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);
}
or:
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;
  Object oTemplatePath = @"C:\test\MyXMLTemplate.dotx";
  oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
  Var Dest=oWordDoc.Bookmarks("BookmarkName").Range;
  {
    For (int i = 0; i < treeViewXMLFiles.Nodes[0].Nodes.Count; i++)
    {
      Dest.InsertParagraphAfter;
      Dest.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Text;
      Dest.InsertParagraphAfter;
      Dest.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[0].Text;
      Dest.InsertParagraphAfter;
      Dest.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[1].Text;
      Dest.InsertParagraphAfter;
      Dest.InsertAfter treeViewXMLFiles.Nodes[0].Nodes[i].Nodes[2].Text; 
      Dest.InsertParagraphAfter;
    }
  }
  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);
}
Since I'm not sure of the C# syntax, I'm attaching a document with some VBA code to demonstrate how you can populate between the paragraphs by using a bookmark. In C# I believe the equivalent of a VBA 'With ... End With' construct is 'Using' or a Var declaration.
Attached Files
File Type: doc Populate Document.doc (44.5 KB, 3 views)
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export VBA form Data to CSV rhys.downard Outlook 0 02-29-2012 07:35 AM
Export Data from Ms project to Ms access using VBA 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
Macro to loop in subfolders, change links, export xml data 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


All times are GMT -7. The time now is 04:20 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft