Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I using excel addin application, to create function which can copy columns from one excel file to another. Here is the code so far but when I render the application, however the program outputs a blank book.xls file.

Updated:
C#
private void button1_Click(object sender, EventArgs e)
       {
           Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
           Excel.Workbook xlWorkBook;
           Excel.Workbook xlWorkBook2;

           Excel.Worksheet xlWorkSheet;
           Excel.Worksheet xlWsheet2;

           Excel.Range xlSourceRange;
           Excel.Range xlSourceRange1;

           Excel.Range xlDestRange;
           Excel.Range xlDestRange1;

           xlWorkBook = xlApp.Workbooks.Open("C:/../../../../../../../Test.xls");


           xlWorkBook2 = xlApp.Workbooks.Open("C:/../../../../../../../Book1.xls");

           //~~> Display Excel
           xlApp.Visible = true;

           //~~> Set the source worksheet
           xlWorkSheet = xlWorkBook.Sheets["Sheet1"];
           //~~> Set the destination worksheet
           xlWsheet2 = xlWorkBook2.Sheets["Sheet1"];

           //~~> Set the source range
           xlSourceRange = xlWorkSheet.Range["E15"].EntireColumn;
           xlSourceRange1 = xlWorkSheet.Range["D15"].EntireColumn;

           //~~> Set the destination range
           xlDestRange = xlWsheet2.Range["A2"];
           xlDestRange1 = xlWsheet2.Range["B2"];


           xlSourceRange.Copy(Type.Missing);

           xlDestRange.PasteSpecial(Excel.XlPasteType.xlPasteAll,
   Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);


           xlSourceRange1.Copy(Type.Missing);

           xlDestRange1.PasteSpecial(Excel.XlPasteType.xlPasteAll,
   Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);

       }



I am little unsure how I to go about tracing for errors, as I am quite novice in this area. Any further assistance would be most appreciated.

Thank you
Posted
Updated 17-Jun-15 3:20am
v3
Comments
CHill60 17-Jun-15 8:21am    
All you are doing is copying ... you're not pasting what you have copied anywhere.
miss786 17-Jun-15 9:22am    
Hi, Thank you for your reply. I have updated my function above with pasteSpecial property, but I am still getting a blank data output in the book1.xls file.

Any further help would be very much appreciated.

1 solution

Couple of things that might help sort this out.
The lines
C#
//~~> Set the destination range
xlDestRange = xlWsheet2.Range["A2"];
xlDestRange1 = xlWsheet2.Range["B2"];
will result in an error
Quote:
The information cannot be pasted because the Copy area and the paste area are not the same size and shape.
You need to select Row 1 if you are trying to paste an EntireColumn
I.e.
C#
xlDestRange = xlWsheet2.Range["A1"];
xlDestRange1 = xlWsheet2.Range["B1"];

The next thing to be wary of is xlPasteSpecialOperationAdd - if there are non-numeric values in your source range then the paste will fail.

Finally - I don't think it's a problem at the moment, but the path to your files is very difficult to follow - I'd advise simplifying the path OR using explicit pathnames
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900