Paste in Excel generate exception

  • Thread starter Thread starter dom_perron
  • Start date Start date
D

dom_perron

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.xlPasteSpecialOperationNone, objFalse,
objTrue);

objSaveChanges = true;

//Restore the old data to the windows clipboard

OPPCEServicesUtilities.Instance.RestoreWindowsClipboardData(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
 
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


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.xlPasteSpecialOperationNone, objFalse,
objTrue);

objSaveChanges = true;

//Restore the old data to the windows clipboard

OPPCEServicesUtilities.Instance.RestoreWindowsClipboardData(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
 
Back
Top