none
Loop through directory and append to file RRS feed

  • Question

  • 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[i], "*.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

    Monday, October 1, 2018 10:20 AM

All replies

  • Hello, 

    You describe few things - folders, files/documents & etc...

    And you have only one function where trying to manage situation.

    Can you separate them?

    Can you create a function which will properly merge two documents?

    Can you create a representation of folder-with-document inside and navigation through documents?

    Can you make those parts work separately? And join a working part into programm? 

    Start with simple thing:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication1
    {
        public class WordDocumentMerge
        {
            public static string Merge(string target, string source)
            {
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
                ...
                return target;
            }
        }
    }

    Make it work as required. 

    Then - look on how to get proper filenames and a call.


    Sincerely, Highly skilled coding monkey.

    Monday, October 1, 2018 10:46 AM
  • 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.

    hmm, your code does not reflect anything of the above. Why not starting with that description turned into code? Something like:

    namespace ConsoleCS
    {
        using System;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                string directory = GetPath();
                List<string> files = EnumFiles(directory);
                WordDocument destination = new WordDocument();
                foreach(string file in files)
                {
                    destination.Merge(file);
                }
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            private static List<string> EnumFiles(string directory)
            {
                throw new NotImplementedException();
            }
    
            private static string GetPath()
            {
                throw new NotImplementedException();
            }
        }
    
        internal class WordDocument
        {
            public WordDocument()
            {
            }
    
            internal void Merge(string file)
            {
                throw new NotImplementedException();
            }
        }
    }

    Monday, October 1, 2018 11:20 AM