G
GE_NK
Guest
I am trying to select a path , then i want to read the directories , in each directory there are multiple word files , i want to open each word file , copy its contents and paste it in a new word documents , example if i have 6 word files in a directory , i want to open a new word document , then open the first document in the directory , copy its contents and paste it to the new document , and loop untill all documents are read.
by far im able to loop over the docs , but the newly created word file is only getting the last opened document ... below is my code hope someone can help !!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
namespace combiner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// For optional parameters create a missing object
object missing = System.Reflection.Missing.Value;
// Create an object of application class
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
// add a document in the Application
Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
// declare variables for setting the position within the document
object start = 0;
object end = 0;
// create a range object which starts at 0
Range rng = adoc.Range(ref start, ref missing);
string path = @"C:\demo";
string[] files = Directory.GetDirectories(path);
for (int i = 0; i < files.Length; i++)
{
string[] ora = Directory.GetFiles(files, "*.docx", SearchOption.AllDirectories);
foreach (string f in ora)
{
MessageBox.Show(f);
textBox1.Text = f.ToString();
// insert a file
rng.InsertFile(textBox1.Text, ref missing, ref missing, ref missing, ref missing);
// now make start to point to the end of the content of the first document
start = WordApp.ActiveDocument.Content.End - 1;
// create another range object with the new value for start
Range rng1 = adoc.Range(ref start, ref missing);
// insert the another document
rng1.InsertFile(textBox1.Text, ref missing, ref missing, ref missing, ref missing);
// now make start to point to the end of the content of the first document
start = WordApp.ActiveDocument.Content.End - 1;
}
// make the word appliction visible
WordApp.Visible = true;
adoc.SaveAs2(@"D:\fulltest.docx");
}
}
}
}
GN
Continue reading...
by far im able to loop over the docs , but the newly created word file is only getting the last opened document ... below is my code hope someone can help !!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
namespace combiner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// For optional parameters create a missing object
object missing = System.Reflection.Missing.Value;
// Create an object of application class
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
// add a document in the Application
Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
// declare variables for setting the position within the document
object start = 0;
object end = 0;
// create a range object which starts at 0
Range rng = adoc.Range(ref start, ref missing);
string path = @"C:\demo";
string[] files = Directory.GetDirectories(path);
for (int i = 0; i < files.Length; i++)
{
string[] ora = Directory.GetFiles(files, "*.docx", SearchOption.AllDirectories);
foreach (string f in ora)
{
MessageBox.Show(f);
textBox1.Text = f.ToString();
// insert a file
rng.InsertFile(textBox1.Text, ref missing, ref missing, ref missing, ref missing);
// now make start to point to the end of the content of the first document
start = WordApp.ActiveDocument.Content.End - 1;
// create another range object with the new value for start
Range rng1 = adoc.Range(ref start, ref missing);
// insert the another document
rng1.InsertFile(textBox1.Text, ref missing, ref missing, ref missing, ref missing);
// now make start to point to the end of the content of the first document
start = WordApp.ActiveDocument.Content.End - 1;
}
// make the word appliction visible
WordApp.Visible = true;
adoc.SaveAs2(@"D:\fulltest.docx");
}
}
}
}
GN
Continue reading...