![]() |
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 |
Copy range values in C#
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.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 |
Copy range values in C#
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.Act iveSheet; Excel.Range exrSource = activeWorksheet.get_Range(strTopSourceCell, strBottomSourceCell); Excel.Range exrDestination = activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell); exrSource.Copy(Type.Missing); exrDestination.PasteSpecial(Microsoft.Office.Inter op.Excel.XlPasteType.xlPas teFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperati onNone, false, false); /* End code snippet */ Regards, Jie Wang , 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: . ================================================== 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. |
Copy range values in C#
Thank you so much. This resolved my issue. I did use
Microsoft.Office.Interop.Excel.XlPasteType.xlPaste Values instead of Microsoft.Office.Interop.Excel.XlPasteType.xlPaste Formulas 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.Act iveSheet; Excel.Range exrSource = activeWorksheet.get_Range(strTopSourceCell, strBottomSourceCell); Excel.Range exrDestination = activeWorksheet.get_Range(strTopDestinationCell, strBottomDestinationCell); exrSource.Copy(Type.Missing); exrDestination.PasteSpecial(Microsoft.Office.Inter op.Excel.XlPasteType.xlPas teFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOpera tion.xlPasteSpecialOperati onNone, false, false); /* End code snippet */ Regards, Jie Wang , 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: . ================================================== 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. |
Copy range values in C#
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 , 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: . ================================================== 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. |
All times are GMT +1. The time now is 04:14 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com