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,
I believe the exception is being thrown because your C# code is creating a
separate Excel.Application object to open each of the workbooks. Instead of
trying to copy a worksheet from a workbook opened in one Excel.Application
instance to a workbook opened in the other Excel.Application instance, you
can use a single Excel.Application instance to open both of the workbooks.
For the following snippet of code:
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);
After changing "App2" to "App1", your code works as expected.
Wb2 = App1.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);
For Office development questions that are not specific to Visual Studio
Tools for the Microsoft Office System, you can also try the officedev forum
at:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.office
dev&lang=en&cr=US
I hope this helps,
McLean Schofield
Programming Writer, Microsoft
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Thank you
Lucy Lu ( guangqin Lu)
my emial:
guang...@citigroup.com
This is my code of C#
-----------------------------------------------------
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
/// </summary>
class myExcel
{
public static int Main()
{
Excel.ApplicationClass exc = new Excel.ApplicationClass();
if (exc == null)
{
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}
exc.Visible = true;
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//To create a workbook with sheets to work on and add more sheets
//////////////////////////////////////////////////////////////////////////////////////////////////////
_Workbook myworkbook = exc.Workbooks.Add(Type.Missing);
Sheets sheets = myworkbook.Worksheets;
_Worksheet worksheet1 = (_Worksheet) sheets.get_Item(1);
worksheet1.Name= "myjob1";
_Worksheet worksheet2 = (_Worksheet) sheets.get_Item(2);
worksheet2.Name="myjob2";
sheets.Add(worksheet1 ,worksheet2 ,5 ,XlSheetType.xlWorksheet); //This
line bombs
Console.WriteLine (sheets.Count);
return 100;
}
}
================================================
This is the error message
================================================
'System.Runtime.InteropServices.COMException' occurred.
Addtional information: Exception from HRESULT:0x800A03EC
The following is the reply you solved for the question put this link.
http://www.ureader.com/message/692318.aspx
==========================================================
Tue, 17 May 2005 17:17:49 GMT
content: Hello,
I believe the exception is being thrown because your C# code is creating a
separate Excel.Application object to open each of the workbooks. Instead of
trying to copy a worksheet from a workbook opened in one Excel.Application
instance to a workbook opened in the other Excel.Application instance, you
can use a single Excel.Application instance to open both of the workbooks.
For the following snippet of code:
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);
After changing "App2" to "App1", your code works as expected.
Wb2 = App1.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);
For Office development questions that are not specific to Visual Studio
Tools for the Microsoft Office System, you can also try the officedev forum
at:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.
office
dev&lang=en&cr=US
I hope this helps,
McLean Schofield
Programming Writer, Microsoft
=======================================================
content: Hello,
I believe the exception is being thrown because your C# code is creating a
separate Excel.Application object to open each of the workbooks. Instead of
trying to copy a worksheet from a workbook opened in one Excel.Application
instance to a workbook opened in the other Excel.Application instance, you
can use a single Excel.Application instance to open both of the workbooks.
For the following snippet of code:
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);
After changing "App2" to "App1", your code works as expected.
Wb2 = App1.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);
For Office development questions that are not specific to Visual Studio
Tools for the Microsoft Office System, you can also try the officedev forum
at:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.
office
dev&lang=en&cr=US
I hope this helps,
McLean Schofield
Programming Writer, Microsoft
=======================================================
It looks like the main problem is that in the following line of code
sheets.Add(worksheet1 ,worksheet2 ,5 ,XlSheetType.xlWorksheet);
you are supplying values for both the 'Before' and 'After' parameters (the
first two parameters). This doesn't make sense to Excel, as you can only
add the new sheets before a specified sheet, or after a specified sheet,
but not both at the same time. You must pick one location for the new
sheets, and then pass System.Type.Missing to the other parameter. So, if
you want to add the new sheets before worksheet1, you could change your
code to:
sheets.Add(worksheet1, System.Type.Missing, 5,
Excel.XlSheetType.xlWorksheet);
Alternatively, if you want to add the new sheets after worksheet2, you
could change your code to:
sheets.Add(System.Type.Missing, worksheet2, 5,
Excel.XlSheetType.xlWorksheet);
If you want to accomplish both (in other words, add 5 new sheets before
worksheet1, and after worksheet2), then you have to make both of these
calls.
Also note that you should not be creating instances of
Microsoft.Office.Interop.Excel.ApplicationClass in your automation code.
Instead, you should create instances of
Microsoft.Office.Interop.Excel.Application. The former works in this case,
but it might cause problems in other code. The following blog entry by Eric
Carter provides further information about the primary interop assembly
(PIA) object model and why you should use
Microsoft.Office.Interop.Excel.Application instead of
Microsoft.Office.Interop.Excel.ApplicationClass:
http://blogs.msdn.com/eric_carter/archive/2004/05/06/127698.aspx
For for further questions such as this that are not specific to Visual
Studio
Tools for the Microsoft Office System, please use the officedev forum at:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.office
dev&lang=en&cr=US
I hope this helps,
McLean Schofield
This posting is provided "AS IS" with no warranties, and confers no rights.
sheets.Add(worksheet1 ,worksheet2 ,5 ,XlSheetType.xlWorksheet);
sheets.Add(worksheet1, System.Type.Missing, 5,
Excel.XlSheetType.xlWorksheet);
sheets.Add(System.Type.Missing, worksheet2, 5,
Excel.XlSheetType.xlWorksheet);
For for further questions such as this that are not specific to Visual
Studio Tools for the Microsoft Office System, please use the officedev
forum at:
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.office
dev&lang=en&cr=US
I hope this helps,
McLean Schofield
This posting is provided "AS IS" with no warranties, and confers no rights.
I am exporting data to excel using the COM object. I got the same error
message (Exception from HRESULT: 0x800A03EC) when exporting text. Does
anyone know if there's any special characters that will generate this
exception when exporting to excel?
--
Harry Miller
This posting is provided "AS IS" with no warranties, and confers no rights.
"david lee" <ab...@yahoo.com> wrote in message
news:9c8146c86b49446b...@ureader.com...
I hope you can help me.
i work on asp 1.1 and i try to create excel document and to fill it with
some data.
i add to my project Microsoft Excel 11.0 Object Library,
Just the only library that i found.
i have this exception when i try to put data into a cell.
Here is my code:
/*************start code ******************************
Dim RetVal As String
Try
Dim app As Excel.Application
Dim exbook As Excel._Workbook
Dim exsheet As Excel._Worksheet
Dim t1 As Type = Type.GetTypeFromProgID("Excel.Application")
Dim tt As XmlElement
Dim columnTitle As String
Dim ColTitel() As String
Dim colCount As Integer
columnTitle = ExcelContent.SelectSingleNode("child::columnTitle")
.InnerText
' colTitle = objXML.documentElement.
selectSingleNode("//columnTitle").text;
'alert(colTitle);
ExcelString = columnTitle
colCount = columnTitle.Split("|").Length - 1
ColTitel = columnTitle.Split("|")
app = CType(Activator.CreateInstance(t1), Excel.Application)
exbook = app.Workbooks.Add(Missing.Value)
exsheet = CType(exbook.ActiveSheet, Excel._Worksheet)
Dim xx, j As Integer
For xx = 0 To colCount
exsheet.Cells(xx, 1) = "sdfsd" ' ColTitel(xx).ToString()
Next
exbook.SaveAs(GetFileName())
exbook.Close(False, Type.Missing, Type.Missing)
app.Quit()
app = Nothing
'app.Quit()
'app.Visible = True
Catch ex As Exception
End Try
/*********************end code ****************************
Maby can someone to help me with this ishue.
Thanks A lot
Didi
as I know all collections in Office are starting from 1, not 0
For xx = 1 To colCount
exsheet.Cells(xx, 1) = "sdfsd" ' ColTitel(xx).ToString()
Next
Hope this helps,
greets, Helmut
"didi" <b_ye...@hotmail.com> schrieb im Newsbeitrag
news:d046a3d129324107...@ureader.com...
workbook.SaveAs(fileName, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.
Missing, false, false, XlSaveAsAccessMode.xlShared,
XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.
Missing, Type.Missing);
I am not having much luck googleing. Any Help would be appreciated.
_variant_t varProgID = _bstr_t("WorkSpace");
_variant_t varPath = vtMissing;
_variant_t varLink(VARIANT_FALSE, VT_BOOL);
_variant_t varDisplayAsIcon(VARIANT_FALSE, VT_BOOL);
Excel::_OLEObjectPtr spOleObject = spOleObjects->Add(
&varProgID, // ProgID
&varPath, //, // FileName
&varLink, // Link
&varDisplayAsIcon, //DisplayAsIcon
10.0F, 10.0F, 400.0F, 150.0F
);
I cought this error and it is throwing 0x800A03EC.
I am using MS office 2007.
//got the error here where macroSample is the name of the module
m_objExcel.Run("macroSample", m_objOpt, m_objOpt, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt,m_objOpt,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt,
m_objOpt, m_objOpt,m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
// this works fine in office 2007. can anyone help me in this issue. am
fighting with this for days