PC Review


Reply
Thread Tools Rate Thread

Copy range values in C#

 
 
BrassicaNigra
Guest
Posts: n/a
 
      16th Mar 2009
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.ActiveSheet;
Excel.Range exrSource =
activeWorksheet.get_Range(strTopSourceCell, strBottomSourceCell);
Globals.ThisAddIn.ThisAddIn_OpenDigiKeyRFQTemplate();
activeWorksheet =
(Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range exrDestination =
activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell);
//exrSource.Copy(exrDestination);
//exrSource.Copy();

exrSource.Copy(exrDestination.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,
Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.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
 
Reply With Quote
 
 
 
 
ryguy7272
Guest
Posts: n/a
 
      16th Mar 2009
Maybe this:
http://www.rondebruin.nl/ado.htm

Or this:
http://www.rondebruin.nl/copy7.htm

I don't know anything about C#.

HTH,
Ryan---

--
RyGuy


"BrassicaNigra" wrote:

> 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.ActiveSheet;
> Excel.Range exrSource =
> activeWorksheet.get_Range(strTopSourceCell, strBottomSourceCell);
> Globals.ThisAddIn.ThisAddIn_OpenDigiKeyRFQTemplate();
> activeWorksheet =
> (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
> Excel.Range exrDestination =
> activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell);
> //exrSource.Copy(exrDestination);
> //exrSource.Copy();
>
> exrSource.Copy(exrDestination.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues,
> Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.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

 
Reply With Quote
 
Jie Wang [MSFT]
Guest
Posts: n/a
 
      17th Mar 2009
Hello Dale,

The PasteSpecial approach is right. Actually you are very close to the
solution now!

First, the Copy method can take zero arguments in VB because it supports
optional parameters, however this is not the story in C#. So we'll have to
pass in something. In this case, it would be Type.Missing.

And we need to specify correct parameters to the PasteSpecial method to
make it work.

I made a little bit changes to your code and please let me know if it works.

/* Begin code snippet */
String strTopSourceCell = "A2", strBottomSourceCell = "A46";
String strTopDestinationCell = "F16", strBottomDestinationCell = "F60";

Excel.Worksheet activeWorksheet =
(Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;

Excel.Range exrSource = activeWorksheet.get_Range(strTopSourceCell,
strBottomSourceCell);

Excel.Range exrDestination =
activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell);

exrSource.Copy(Type.Missing);
exrDestination.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPas
teFormulas,

Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperati
onNone,
false,
false);
/* End code snippet */

Regards,

Jie Wang ((E-Mail Removed), remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
BrassicaNigra
Guest
Posts: n/a
 
      17th Mar 2009
Thank you so much. This resolved my issue. I did use

Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues

instead of

Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas

because all I want to copy is the current value in the cell.

""Jie Wang [MSFT]"" wrote:

> Hello Dale,
>
> The PasteSpecial approach is right. Actually you are very close to the
> solution now!
>
> First, the Copy method can take zero arguments in VB because it supports
> optional parameters, however this is not the story in C#. So we'll have to
> pass in something. In this case, it would be Type.Missing.
>
> And we need to specify correct parameters to the PasteSpecial method to
> make it work.
>
> I made a little bit changes to your code and please let me know if it works.
>
> /* Begin code snippet */
> String strTopSourceCell = "A2", strBottomSourceCell = "A46";
> String strTopDestinationCell = "F16", strBottomDestinationCell = "F60";
>
> Excel.Worksheet activeWorksheet =
> (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
>
> Excel.Range exrSource = activeWorksheet.get_Range(strTopSourceCell,
> strBottomSourceCell);
>
> Excel.Range exrDestination =
> activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell);
>
> exrSource.Copy(Type.Missing);
> exrDestination.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPas
> teFormulas,
>
> Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperati
> onNone,
> false,
> false);
> /* End code snippet */
>
> Regards,
>
> Jie Wang ((E-Mail Removed), remove 'online.')
>
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> (E-Mail Removed).
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
> Note: MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 2 business days is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions. Issues of this
> nature are best handled working with a dedicated Microsoft Support Engineer
> by contacting Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
Jie Wang [MSFT]
Guest
Posts: n/a
 
      18th Mar 2009
Hi Dale,

Great to see now it works!

If you have any further questions, please feel free to post here and we'll
be happy to help.

Have a nice day!

Best regards,

Jie Wang ((E-Mail Removed), remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy range and paste as values Tom Joseph Microsoft Excel Programming 1 1st Mar 2009 12:16 PM
Re: copy and paste values in a range Dave Peterson Microsoft Excel Programming 4 14th Dec 2006 10:17 PM
copy / paste values for certain range Eric Dreshfield Microsoft Excel Programming 2 10th Sep 2003 04:04 PM
Range COPY function - how to copy VALUES and not formulas James Cooke Microsoft Excel Programming 1 21st Aug 2003 07:04 PM
Range COPY function - how to copy VALUES and not formulas James Cooke Microsoft Excel Worksheet Functions 1 21st Aug 2003 07:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:20 AM.