By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Paste in Excel generate exception

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.