J
james
Hi all, I am trying to convert some old VB6 code to .NET.
Take this first section (there is more, but hey-ho...)
Dim XLApp As Object
Dim XLSheet
Dim XLBook
Dim HeaderItem As String
Dim ColCount As Integer
ColCount = 1
Set XLApp = CreateObject("EXCEL.Application")
XLApp.Visible = False
XLApp.Workbooks.Open (ExcelFile)
Now, after googling, I have found I can either add a reference to Excel, or
I can use the "GetTypeFromProgID" method instead. The latter is preferable,
as there is no guarantee of the end user having a specific version of Excel,
and the "createobject" method didn't seem to care.
So far, I have this:
//Create Excel object
Object XLApp;
Object XLWorkBook;
Object XLSheet;
Object XLBook;
String HeaderItem;
Int16 ColCount = 1;
Type ExcelType = Type.GetTypeFromProgID("EXCEL.Application");
lstExcelProgress.Items.Add("Creating Excel instance...");
try
{
XLApp = Activator.CreateInstance(ExcelType);
ExcelType.InvokeMember("Visible",
System.Reflection.BindingFlags.SetProperty, null, XLApp, new object[] {
false });
lstExcelProgress.Items.Add("Trying to open the workbook...");
try
{
XLWorkBook = ExcelType.InvokeMember("Workbooks.Open",
System.Reflection.BindingFlags.InvokeMethod, null, XLApp, new object[]
{ExcelFile});
}
catch (Exception wer)
{
//handle error }
}
catch (Exception er)
{
//handle error
}
Now, the first bit works OK where I create my Excel instance, but the second
bit (opening the workbook) is failing, the error returned being Exception
from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME).
I assume it doesn't like Workbooks.Open as a method to run, but I'm not sure
how else to do it? Any ideas?
James.
Take this first section (there is more, but hey-ho...)
Dim XLApp As Object
Dim XLSheet
Dim XLBook
Dim HeaderItem As String
Dim ColCount As Integer
ColCount = 1
Set XLApp = CreateObject("EXCEL.Application")
XLApp.Visible = False
XLApp.Workbooks.Open (ExcelFile)
Now, after googling, I have found I can either add a reference to Excel, or
I can use the "GetTypeFromProgID" method instead. The latter is preferable,
as there is no guarantee of the end user having a specific version of Excel,
and the "createobject" method didn't seem to care.
So far, I have this:
//Create Excel object
Object XLApp;
Object XLWorkBook;
Object XLSheet;
Object XLBook;
String HeaderItem;
Int16 ColCount = 1;
Type ExcelType = Type.GetTypeFromProgID("EXCEL.Application");
lstExcelProgress.Items.Add("Creating Excel instance...");
try
{
XLApp = Activator.CreateInstance(ExcelType);
ExcelType.InvokeMember("Visible",
System.Reflection.BindingFlags.SetProperty, null, XLApp, new object[] {
false });
lstExcelProgress.Items.Add("Trying to open the workbook...");
try
{
XLWorkBook = ExcelType.InvokeMember("Workbooks.Open",
System.Reflection.BindingFlags.InvokeMethod, null, XLApp, new object[]
{ExcelFile});
}
catch (Exception wer)
{
//handle error }
}
catch (Exception er)
{
//handle error
}
Now, the first bit works OK where I create my Excel instance, but the second
bit (opening the workbook) is failing, the error returned being Exception
from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME).
I assume it doesn't like Workbooks.Open as a method to run, but I'm not sure
how else to do it? Any ideas?
James.