Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #9  
Old 04-18-2014, 04:17 AM
macropod's Avatar
macropod macropod is online now Export data from C# winForms treeView to MS-Word MergeFields Windows 7 32bit Export data from C# winForms treeView to MS-Word MergeFields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
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, 11 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
 

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 C# winForms treeView to MS-Word MergeFields 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
Export data from C# winForms treeView to MS-Word MergeFields 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft