View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
BrassicaNigra BrassicaNigra is offline
external usenet poster
 
Posts: 2
Default Copy range values in C#

Greetings,

There are several examples of what I am trying to do in this section but
they all appear to use VB. I have an Excel add-in written in C#. One thing
I need to do is copy the values from a range of cells into a different range
of cells in another workbook. Here is the code;


private void Go_Click(object sender, EventArgs e)
{
String strTopSourceCell = "A2", strBottomSourceCell = "A46";
String strTopDestinationCell = "F16", strBottomDestinationCell =
"F60";
Excel.Worksheet activeWorksheet =
(Excel.Worksheet)Globals.ThisAddIn.Application.Act iveSheet;
Excel.Range exrSource =
activeWorksheet.get_Range(strTopSourceCell, strBottomSourceCell);
Globals.ThisAddIn.ThisAddIn_OpenDigiKeyRFQTemplate ();
activeWorksheet =
(Excel.Worksheet)Globals.ThisAddIn.Application.Act iveSheet;
Excel.Range exrDestination =
activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell);
//exrSource.Copy(exrDestination);
//exrSource.Copy();

exrSource.Copy(exrDestination.PasteSpecial(Microso ft.Office.Interop.Excel.XlPasteType.xlPasteValues,
Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperationNone, true, false));
}

When I use the (commented) line exrSrouce.Copy(exrDestination) I get the
values but the format of the destination is being altered.


When I use the (commented) line exrSrouce.Copy() I get a compiler error - no
overload for range.copy has zero arguments.

The documentation for Range.Copy() said that if the destination argument was
omitted it would copy to the clipboard. If I put null in there I get a run
time error.

I have tried using the Range.PasteSpecial as an argument for copy (the
uncommented line in my code) and it throws a runtime error. I suspect it is
to be used after the clipboard has been loaded.

How do I copy this range to the clipboard and then paste just the values to
the destination range (in C#).

Thanks for your help.

Dale Hoffman