This project has moved. For the latest updates, please go here.

Copy Tables from Doc to Excel without using clipboard

Feb 1, 2015 at 3:56 PM
Edited Feb 1, 2015 at 3:59 PM
I had couple of word doc file containing multiple tables in it. My task was to convert them [ appending top down ] to xls. I had used NetOffice with C# for the same.

For that I created Excel App and Word App and selected each tables one by one and pasted it in new sheet.

One known issue is that it was sharing clipboard to do the job. I would like to do it without using system clipboard but did not got any further improvement idea. If anyone can help me on that it'll be excellent for me.

Also I want to create a console app for this code and want to call is from R or java via system call.
Please tell me whether it will work or not. If not then suggest me some way to do so.

Here is the code :-
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



using NetOffice;
using Excel = NetOffice.ExcelApi;
using Word = NetOffice.WordApi;

namespace Word_To_XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            // start excel and word and turn off msg boxes
            Excel.Application excelApplication = new Excel.Application();
            excelApplication.DisplayAlerts = false;

            Word.Application wordApplication = new Word.Application();
            wordApplication.DisplayAlerts = NetOffice.WordApi.Enums.WdAlertLevel.wdAlertsNone;


            string[] filePaths = Directory.GetFiles(@"C:\tmp_Doc_to_XLS", "*.doc");


            //Path.GetFileNameWithoutExtension(filePaths[1]);

            for (int doc_fn = 0; doc_fn < filePaths.Length; doc_fn++)
            {
                if (!IsFileLocked(new FileInfo(filePaths[doc_fn])) && !((File.GetAttributes(filePaths[doc_fn]) & FileAttributes.Hidden) == FileAttributes.Hidden))
                {

                    Excel.Workbook workBook = excelApplication.Workbooks.Add();
                    Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];


                    // add a new document
                    Word.Document word_doc = wordApplication.Documents.Open(filePaths[doc_fn]);


                    int num_tables = word_doc.Tables.Count;

                    if (num_tables > 0)
                    {
                        for (int i = 1; i <= num_tables; i++)
                        {
                            Word.Table table = word_doc.Tables[i];

                            table.Range.Copy();

                            Excel.Range last = workSheet.Cells.SpecialCells(NetOffice.ExcelApi.Enums.XlCellType.xlCellTypeLastCell);
                            //Excel.Range range = workSheet.get_Range("A1", last);

                            workSheet.Cells[last.Row + 2, 1].Activate();

                            //workSheet.UsedRange.End(NetOffice.ExcelApi.Enums.XlDirection.xlDown).Select();

                            workSheet.Paste();

                            // kept for later :- workSheet.PasteSpecial(NetOffice.ExcelApi.Enums.XlPasteType.xlPasteValues, NetOffice.ExcelApi.Enums.XlPasteSpecialOperation.xlPasteSpecialOperationNone);            
                        }
                    }




                    // save the book 
                    workBook.SaveAs(Path.GetDirectoryName(filePaths[doc_fn]) + "\\" + Path.GetFileNameWithoutExtension(filePaths[doc_fn]) + ".xls", NetOffice.ExcelApi.Enums.XlFileFormat.xlWorkbookNormal);

                    workBook.Close();
                    workBook.Dispose();
                }


            }


            // close word and dispose reference
            wordApplication.Quit();
            wordApplication.Dispose();
            // close excel and dispose reference
            excelApplication.Quit();
            excelApplication.Dispose();
        }

        public static Boolean IsFileLocked(FileInfo file)
        {
            FileStream stream = null;

            try
            {
                //Don't change FileAccess to ReadWrite, 
                //because if a file is in readOnly, it fails.
                stream = file.Open
                (
                    FileMode.Open,
                    FileAccess.Read,
                    FileShare.None
                );
            }
            catch (IOException)
            {
                //the file is unavailable because it is:
                //still being written to
                //or being processed by another thread
                //or does not exist (has already been processed)
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            //file is not locked
            return false;
        }
    }
}