Mike
2005-05-16 21:29:02 UTC
Hi! I already posted this question but has gotten nowhere yet. I want to copy
one worksheet into another spreadsheet. Ron de Bruin from MS Excel
Programming
forum has suggested to use this code to achieve this:
Sub test()
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Application.ScreenUpdating = False
Set Wb1 = Application.Workbooks.Open("C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test1.xls")
Set Wb1 = ActiveWorkbook
Set Wb2 = Application.Workbooks.Open("C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test2.xls")
Wb2.Sheets("Template").Copy After:=Wb1.Sheets(Wb1.Sheets.Count)
Wb2.Close False
Wb1.Save
Wb1.Close False
Set Wb2 = Nothing
Set Wb1 = Nothing
Application.ScreenUpdating = True
'clean up
End Sub
The code works great in VB-6. Here's my port in C#:
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Workbook Wb1 = null;
Excel.Workbook Wb2 = null;
Excel.Worksheet Ws1 = null;
Excel.Application App1 = null;
Excel.Application App2 = null;
try
{
//App1 = new Excel.Application();
//App2 = new Excel.Application();
Type t1 = Type.GetTypeFromProgID("Excel.Application");
App1 = (Excel.Application) Activator.CreateInstance(t1);
Type t2 = Type.GetTypeFromProgID("Excel.Application");
App2 = (Excel.Application) Activator.CreateInstance(t2);
App1.ScreenUpdating = false;
Wb1 = App1.Workbooks.Open(@"C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test1.xls",
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Wb1 = App1.ActiveWorkbook;
Wb2 = App2.Workbooks.Open(@"C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test2.xls",
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Ws1 = (Excel.Worksheet) Wb2.Sheets.get_Item("Template");
// Bombs on this line:
Ws1.Copy(Type.Missing,Wb1.Sheets.get_Item(Wb1.Sheets.Count));
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
MessageBox.Show(ex.Message);
}
finally
{
Wb1.Save();
App1.ScreenUpdating = true;
App1.Quit();
App1 = null;
Wb2.Close(false,Type.Missing,Type.Missing);
App2.Quit();
App2 = null;
Application.Exit();
this.Close();
}
}
When I run this code it bombs with the following error:
'DefaultDomain': Loaded
'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON': Loaded 'C:\Documents and Settings\conmxz\My
Documents\Visual Studio
Projects\EXCEL_MERGER_DELETE_SOON\EXCEL_MERGER_DELETE_SOON\bin\Debug\EXCEL_MERGER_DELETE_SOON.exe', Symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No
symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\microsoft.office.interop.excel\11.0.0.0__71e9bce111e9429c\microsoft.office.interop.excel.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
System.Runtime.InteropServices.COMException (0x800A03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Copy(Object Before, Object
After)
at EXCEL_MERGER_DELETE_SOON.Form1.button1_Click(Object sender, EventArgs
e) in c:\documents and settings\conmxz\my documents\visual studio
projects\excel_merger_delete_soon\excel_merger_delete_soon\form1.cs:line 129
What am I doing wrong ? Is this a bug ? I googled a bit but did not find
anything usefull.
Any help is greatly appreciated.
Thank you in advance,
one worksheet into another spreadsheet. Ron de Bruin from MS Excel
Programming
forum has suggested to use this code to achieve this:
Sub test()
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Application.ScreenUpdating = False
Set Wb1 = Application.Workbooks.Open("C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test1.xls")
Set Wb1 = ActiveWorkbook
Set Wb2 = Application.Workbooks.Open("C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test2.xls")
Wb2.Sheets("Template").Copy After:=Wb1.Sheets(Wb1.Sheets.Count)
Wb2.Close False
Wb1.Save
Wb1.Close False
Set Wb2 = Nothing
Set Wb1 = Nothing
Application.ScreenUpdating = True
'clean up
End Sub
The code works great in VB-6. Here's my port in C#:
private void button1_Click(object sender, System.EventArgs e)
{
Excel.Workbook Wb1 = null;
Excel.Workbook Wb2 = null;
Excel.Worksheet Ws1 = null;
Excel.Application App1 = null;
Excel.Application App2 = null;
try
{
//App1 = new Excel.Application();
//App2 = new Excel.Application();
Type t1 = Type.GetTypeFromProgID("Excel.Application");
App1 = (Excel.Application) Activator.CreateInstance(t1);
Type t2 = Type.GetTypeFromProgID("Excel.Application");
App2 = (Excel.Application) Activator.CreateInstance(t2);
App1.ScreenUpdating = false;
Wb1 = App1.Workbooks.Open(@"C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test1.xls",
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Wb1 = App1.ActiveWorkbook;
Wb2 = App2.Workbooks.Open(@"C:\Documents and
Settings\conmxz\Desktop\EXCEL_MERGE\test2.xls",
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Ws1 = (Excel.Worksheet) Wb2.Sheets.get_Item("Template");
// Bombs on this line:
Ws1.Copy(Type.Missing,Wb1.Sheets.get_Item(Wb1.Sheets.Count));
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
MessageBox.Show(ex.Message);
}
finally
{
Wb1.Save();
App1.ScreenUpdating = true;
App1.Quit();
App1 = null;
Wb2.Close(false,Type.Missing,Type.Missing);
App2.Quit();
App2 = null;
Application.Exit();
this.Close();
}
}
When I run this code it bombs with the following error:
'DefaultDomain': Loaded
'c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON': Loaded 'C:\Documents and Settings\conmxz\My
Documents\Visual Studio
Projects\EXCEL_MERGER_DELETE_SOON\EXCEL_MERGER_DELETE_SOON\bin\Debug\EXCEL_MERGER_DELETE_SOON.exe', Symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll', No
symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\microsoft.office.interop.excel\11.0.0.0__71e9bce111e9429c\microsoft.office.interop.excel.dll', No symbols loaded.
'EXCEL_MERGER_DELETE_SOON.exe': Loaded
'c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll', No symbols loaded.
System.Runtime.InteropServices.COMException (0x800A03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Copy(Object Before, Object
After)
at EXCEL_MERGER_DELETE_SOON.Form1.button1_Click(Object sender, EventArgs
e) in c:\documents and settings\conmxz\my documents\visual studio
projects\excel_merger_delete_soon\excel_merger_delete_soon\form1.cs:line 129
What am I doing wrong ? Is this a bug ? I googled a bit but did not find
anything usefull.
Any help is greatly appreciated.
Thank you in advance,