.NET Interop Copy all cells from Worksheet to Clipboard

A

andalmeida

Hey,

Does anyone know how to copy all cell contents from a Sheet to the clipboard?

I tried:

Excel.ApplicationClass oExcelApp;
oExcelApp = new Excel.ApplicationClass();
oExcelApp.DisplayAlerts = false;
oExcelApp.Workbooks.Open(fileName.ToString(),
System.Reflection.Missing.Value, false, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, System.Reflection.Missing.Value, true,
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, false, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
System.Reflection.Missing.Value);

Excel.Range range = oExcelApp.Cells;
range.Select();
object dest = new object();
range.Copy(dest); <- i got a nice Exception here :-(

Thanks Dudes
 
N

NickHK

If would guess it is because you are not specifying a valid range. In VBA
Dim CopyRange As Range
Set CopyRange = Worksheets(1).Cells
CopyRange.Copy Worksheets(2).Range("A1")

NickHK
 
A

andalmeida

thanks, so i tried, but failed:

Excel.Range range = oExcelApp.Cells;
range.Copy("A1");

and got same exception:
"Copy method of Range class failed"
" at System.RuntimeType.ForwardCallToInvokeMember(String memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)\r\n at Excel.Range.Copy(Object Destination)\r\n at
xlsconcat.Form1.button2_Click(Object sender, EventArgs e) in
C:\\Anderson\\xlsconcat\\Form1.cs:line 53\r\n at
System.Windows.Forms.Control.OnClick(EventArgs e)\r\n at
System.Windows.Forms.Button.OnClick(EventArgs e)\r\n at
System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n at
System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32
clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n at
System.Windows.Forms.Button.WndProc(Message& m)\r\n at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)\r\n at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)\r\n at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.Application.Run(Form mainForm)\r\n at
xlsconcat.Program.Main() in C:\\Anderson\\xlsconcat\\Program.cs:line 17"
 
N

NickHK

No idea about the .Net stuff, but if you receive all those errors for a copy
statement, you have some work to do.
Check the documentation for the Copy method and see what it expects as a
destination; a string, range or what. With ( ) or without.

NickHK

andalmeida said:
thanks, so i tried, but failed:

Excel.Range range = oExcelApp.Cells;
range.Copy("A1");

and got same exception:
"Copy method of Range class failed"
" at System.RuntimeType.ForwardCallToInvokeMember(String memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)\r\n at Excel.Range.Copy(Object Destination)\r\n at
xlsconcat.Form1.button2_Click(Object sender, EventArgs e) in
C:\\Anderson\\xlsconcat\\Form1.cs:line 53\r\n at
System.Windows.Forms.Control.OnClick(EventArgs e)\r\n at
System.Windows.Forms.Button.OnClick(EventArgs e)\r\n at
System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n at
System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32
clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n at
System.Windows.Forms.Button.WndProc(Message& m)\r\n at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)\r\n at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n atSystem.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsaf
eNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)\r\n at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)\r\n at
System.Windows.Forms.Application.Run(Form mainForm)\r\n at
xlsconcat.Program.Main() in C:\\Anderson\\xlsconcat\\Program.cs:line 17"

NickHK said:
If would guess it is because you are not specifying a valid range. In VBA
Dim CopyRange As Range
Set CopyRange = Worksheets(1).Cells
CopyRange.Copy Worksheets(2).Range("A1")

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top