473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Paste in Excel generate exception

Hi,

I have a problem when I want to paste data from a workbook to another
in excel using the clipboard. Each time a try to paste the data in the
active sheet I got an exception "Microsoft Office Excel cannot paste
the data." I tried 3 differents method but I always got the
exception.

Here is my code:

ApplicationClass excelApp = new ApplicationClass();

try
{
// Here is the way to handle parameters you don't care
about in .NET
object missing = Type.Missing;

// Workaround to open Excel. Set the english during
opening
CultureInfo oldCulture =
Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new
CultureInfo("en-US");

Workbook workbook = excelApp.Workbooks.Open(i_url,
missing, missing, missing, missing,
missing,
missing, missing, missing, missing,
missing,
missing, missing, missing, missing);

object objSaveChanges = false;

if (workbook.ReadOnly == false)
{
//To find the cells of the filesection we have to
extract it from the bookmark
// Hte bookmark structure is
// <sheetname>-<topleft cell>:<bottomright cell>
string[] strArray = i_bookmark.Split(new Char[]
{ '-' });

string sheetName = strArray[0];
string cellAdress = strArray[1];

string[] cellArray = cellAdress.Split(new Char[]
{ ':' });

_Worksheet sheet =
(_Worksheet)excelApp.Sheets[sheetName];
sheet.Select(missing);

Range cellRange = sheet.get_Range(cellArray[0],
cellArray[1]);

//Create a new object to set to
Clipboard
DataObject newDataObject = new DataObject();
newDataObject.SetData("HTML", i_newContent);

//Set Data to clipboard
Clipboard.SetDataObject(newDataObject, false);

//object objFalse = false;
//object objFormat = "HTML";
//object objTrue = true;
//object objRange = cellRange as object;
//Replace the selection with the clipboard content
cellRange.Select();
sheet.Paste(cellRange, false);
//sheet.Paste();

//sheet.PasteSpecial(objFormat, objFalse,
objFalse, missing, missing, missing, objFalse);
//cellRange.PasteSpecial(XlPasteType.xlPasteAll,
XlPasteSpecialOperation.xlPasteSpecialOperationNon e, objFalse,
objTrue);

objSaveChanges = true;

//Restore the old data to the windows clipboard

OPPCEServicesUtilities.Instance.RestoreWindowsClip boardData(m_backupClipboard);

}
else
{
MessageBox.Show("File in read-only. Cannot replace
section.");
}
// Reset culture to the previous one.
Thread.CurrentThread.CurrentCulture = oldCulture;
//Save the woorkbook to keep changes
object objFileName = i_url;
workbook.Close(objSaveChanges, objFileName, missing);


Thanks in advance for any help.

Dominique

Jul 4 '07 #1
1 5526
I think this was/is a bug if memory serves me correctly. Have a google for a
patch.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
<do********@hotmail.comwrote in message
news:11*********************@n2g2000hse.googlegrou ps.com...
Hi,

I have a problem when I want to paste data from a workbook to another
in excel using the clipboard. Each time a try to paste the data in the
active sheet I got an exception "Microsoft Office Excel cannot paste
the data." I tried 3 differents method but I always got the
exception.

Here is my code:

ApplicationClass excelApp = new ApplicationClass();

try
{
// Here is the way to handle parameters you don't care
about in .NET
object missing = Type.Missing;

// Workaround to open Excel. Set the english during
opening
CultureInfo oldCulture =
Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new
CultureInfo("en-US");

Workbook workbook = excelApp.Workbooks.Open(i_url,
missing, missing, missing, missing,
missing,
missing, missing, missing, missing,
missing,
missing, missing, missing, missing);

object objSaveChanges = false;

if (workbook.ReadOnly == false)
{
//To find the cells of the filesection we have to
extract it from the bookmark
// Hte bookmark structure is
// <sheetname>-<topleft cell>:<bottomright cell>
string[] strArray = i_bookmark.Split(new Char[]
{ '-' });

string sheetName = strArray[0];
string cellAdress = strArray[1];

string[] cellArray = cellAdress.Split(new Char[]
{ ':' });

_Worksheet sheet =
(_Worksheet)excelApp.Sheets[sheetName];
sheet.Select(missing);

Range cellRange = sheet.get_Range(cellArray[0],
cellArray[1]);

//Create a new object to set to
Clipboard
DataObject newDataObject = new DataObject();
newDataObject.SetData("HTML", i_newContent);

//Set Data to clipboard
Clipboard.SetDataObject(newDataObject, false);

//object objFalse = false;
//object objFormat = "HTML";
//object objTrue = true;
//object objRange = cellRange as object;
//Replace the selection with the clipboard content
cellRange.Select();
sheet.Paste(cellRange, false);
//sheet.Paste();

//sheet.PasteSpecial(objFormat, objFalse,
objFalse, missing, missing, missing, objFalse);
//cellRange.PasteSpecial(XlPasteType.xlPasteAll,
XlPasteSpecialOperation.xlPasteSpecialOperationNon e, objFalse,
objTrue);

objSaveChanges = true;

//Restore the old data to the windows clipboard

OPPCEServicesUtilities.Instance.RestoreWindowsClip boardData(m_backupClipboard);

}
else
{
MessageBox.Show("File in read-only. Cannot replace
section.");
}
// Reset culture to the previous one.
Thread.CurrentThread.CurrentCulture = oldCulture;
//Save the woorkbook to keep changes
object objFileName = i_url;
workbook.Close(objSaveChanges, objFileName, missing);


Thanks in advance for any help.

Dominique

Jul 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: zxo102 | last post by:
Hi there, I need your help for python <--> excel. I want to paste selected cells (range) to different location on the same sheet in Excel through python. I have tried it for a while but could not...
3
by: Dent2 | last post by:
I wrote a nice C# excel routine to automate the formatting of some raw CSV data. I wrote the routine on a WinXP computer with Visual Studio .NET 2003 and Office XP installed. It compiled and ran...
3
by: hari krishna | last post by:
hi, I am generating excel reports through vb.Net. After creating excel.application and the report is generated, each report leaves Excel in memory. I can see them in task manager in Process tab...
9
by: hari krishna | last post by:
hi, I want to send the data from dataset information to Excel through ASP.Net. there will be no XL installed on web server. web server is win 2000 server machine. I am using visual basic code in...
7
by: Alain \Mbuna\ | last post by:
Hi everybody. In my program I have some data that is calculated after some input from the user. I have written some code that opens an Excel workbook, with 5 worksheets and the calculated data...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
0
by: shantanu | last post by:
I am trying to convert a macro code to c# that will copy the values of a column and paste to anather through paste special. Everything is working fine but the transpose meathod to paste the column...
1
by: DennisBetten | last post by:
First of all, I need to give some credit to Mahesh Chand for providing me with an excellent basis to export data to excel. What does this code do: As the title says, this code is capable of...
24
rauty
by: rauty | last post by:
Hi all, I'm trying to access the most recent instance of Excel so I can paste data. What works for me is if Excel isn't already opened, I open it and can paste the data where I want to. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.