473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Excel COM object

I wrote a nice C# excel routine to automate the formatting of some raw CSV
data. I wrote the
routine on a WinXP computer with Visual Studio .NET 2003 and Office XP
installed. It compiled and ran on my XP machine without problems. Then, I
tried to run the program on a Windows 2000 computer with Office 2000
installed. I got an error:

System.IO.FileNotFoundException: File or assembly name
"Microsoft.Office.Interop.Excel, or one of its dependencies, was not found."

I think that this problem is occurring because the program is compiled on my
XP/Office
10.0 COM-Object system and so it doesn't include all the previous version
objects that are needed to make use of earlier versions of Excel.

My current code employs early-binding of the excel object. I tried to use
late-binding in hopes that this would solve my problem, but I have been
unable to make this work. My trials at late-binding fail durinng
compilation when the Excel.Application() object is created. The compiler
tells me that there is no reference to Excel. My understanding is that, in
late binding, the Excel COM object is created and compiled during the
execution of the program. It shouldn't need a referencing 'using Excel'
statement.

Has anyone in this newsgroup had experience with this problem? Perhaps,
you've dealt with this, or you can refer me to another newsgroup where
someone might be more knowledgable about this issue.

Thank you,
Dennis
Nov 15 '05 #1
3 9109
> System.IO.FileNotFoundException: File or assembly name
"Microsoft.Office.Interop.Excel, or one of its dependencies, was not

found."

if you use Excel.Application in your .NET code, you assume to have a
COMInterop assembly generated from Excel type library. just look for it in
the /debug or /release folder and distribute it with your application. that
way the early binding will work.

however, if you wish to use late binding, you do not have to use
Excel.Application object. in fact, you should not use it at all! instead,
create a type that correspond to it, create object of the type and invoke
its metods/properties. it is much easier to work with late binding with
VB.NET, though, because vb.net supports late binding at language level.

Regards, Wiktor

// c# code
try
{
Type t = Type.GetTypeFromProgID( "Word.Application" );
object w = Activator.CreateInstance( t );

// w.Visible = true
t.InvokeMember( "Visible", BindingFlags.SetProperty,
null, w, new Object[] { true } );

// w.Documents...
object docs = t.InvokeMember( "Documents", BindingFlags.GetProperty,
null, w, null );
// ...Add
t.InvokeMember( "Add", BindingFlags.InvokeMethod,
null, docs, null );

w = null;
}
catch( TypeLoadException ex )
{
...
}

// vb.net code
dim o as object
o = CreateObject( "Word.Application" )
o.Visible = True
o.Documents.Add

Nov 15 '05 #2
Wiktor,
if you use Excel.Application in your .NET code, you assume to have a
COMInterop assembly generated from Excel type library. just look for it in
the /debug or /release folder and distribute it with your application. that way the early binding will work.
I was not able to get my early-binding code to work. In short, here is the
code:

c#
using Microsoft.Office.Interop.Excel;
using excel = Microsoft.Office.Interop.Excel;
class GetInventory
{
public static void Main()
{
try
{
excel.Application ExcelObj = new excel.Application();
... (working with ExcelObj)

System.Runtime.InteropServices.Marshal.ReleaseComO bject(ExcelObj);
ExcelObj = null;
}
catch(IOException e) //not sure about this catch statement. I need
to study my exception handling.
{
...
}
This is the code that generated the "System.IO.FileNotFoundException" that I
wrote about in my first posting. I looked for the COMInterop assembly in
the locations you suggested, but I didn't find anything. Do I need to use a
specific compiler switch to generate this assembly? I compiled with the
"Build" on the toolbar of Visual Studio 2003. What kind of file extension
am I looking for on that COMInterop assembly?

I tried installing the PIAs for XP on the 2000 machine, but this was a
mistake. I also tried copying Interop.excel.dll and
microsoft.office.interop.excel.dll to that machine and registering them with
regsvr32. This was also an obvious mistake because on both attempts to
register, I got the error

".dll was loaded, but the DLLRegServer entry point was not found.
DllRegisterServer may not be exported, or a corrupt version of
interop.excel.dll may be in memory. Consider using pview to detect and
remove it."

It was a shot in the dark on my part.
if you wish to use late binding, you do not have to use
Excel.Application object. in fact, you should not use it at all! instead,
create a type that correspond to it, create object of the type and invoke
its metods/properties. it is much easier to work with late binding with
VB.NET, though, because vb.net supports late binding at language level.


With regards to the late binding example, thank you. It works just as you
said. I had tried a couple of other examples, but they didn't work. From
your example, I expect to be able to build something useful.

Thank you,
Dennis
Nov 15 '05 #3
I have also followed up your late binding code , its great because
earlier I was creating the ApplicationClass instance

but with this code I get the following exception at the add line

I am using Excel instead of Word
the code is

Type t = Type.GetTypeFromProgID( "Excel.Application" );
object w = Activator.CreateInstance( t );
// Visible True
t.InvokeMember( "Visible", BindingFlags.SetProperty,null, w, new
Object[] { true } );
// w WorkBooks
object docs = t.InvokeMember( "WorkBooks",
BindingFlags.GetProperty,null, w, null );
// w Add method
t.InvokeMember( "Add", BindingFlags.InvokeMethod,null, docs, null );
w = null;

the exception is

"System.Reflection.TargetException: Object does not match target
type.\r\n at System.RuntimeType.InvokeDispMethod(String name,
BindingFlags invokeAttr, Object target, Object[] args, Boolean[]
byrefModifiers, Int32 culture, String[] namedParameters)\r\n at
System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr,
Binder binder, Object target, Object[] args, ParameterModifier[]
modifiers, CultureInfo culture, String[] namedParameters)\r\n at
System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder
binder, Object target, Object[] args)\r\n at
ExcelTry2.ExcelInterOp.Init() in c:\\documents and settings\\champ\\my
documents\\visual studio
projects\\exceltry2\\exceltry2\\excelinterop.cs:li ne 41"
"Dent" <dt******@hotmail.com> wrote in message news:<uh**************@TK2MSFTNGP12.phx.gbl>...
Wiktor,
if you use Excel.Application in your .NET code, you assume to have a
COMInterop assembly generated from Excel type library. just look for it in
the /debug or /release folder and distribute it with your application.

that
way the early binding will work.


I was not able to get my early-binding code to work. In short, here is the
code:

c#
using Microsoft.Office.Interop.Excel;
using excel = Microsoft.Office.Interop.Excel;
class GetInventory
{
public static void Main()
{
try
{
excel.Application ExcelObj = new excel.Application();
... (working with ExcelObj)

System.Runtime.InteropServices.Marshal.ReleaseComO bject(ExcelObj);
ExcelObj = null;
}
catch(IOException e) //not sure about this catch statement. I need
to study my exception handling.
{
...
}
This is the code that generated the "System.IO.FileNotFoundException" that I
wrote about in my first posting. I looked for the COMInterop assembly in
the locations you suggested, but I didn't find anything. Do I need to use a
specific compiler switch to generate this assembly? I compiled with the
"Build" on the toolbar of Visual Studio 2003. What kind of file extension
am I looking for on that COMInterop assembly?

I tried installing the PIAs for XP on the 2000 machine, but this was a
mistake. I also tried copying Interop.excel.dll and
microsoft.office.interop.excel.dll to that machine and registering them with
regsvr32. This was also an obvious mistake because on both attempts to
register, I got the error

".dll was loaded, but the DLLRegServer entry point was not found.
DllRegisterServer may not be exported, or a corrupt version of
interop.excel.dll may be in memory. Consider using pview to detect and
remove it."

It was a shot in the dark on my part.
if you wish to use late binding, you do not have to use
Excel.Application object. in fact, you should not use it at all! instead,
create a type that correspond to it, create object of the type and invoke
its metods/properties. it is much easier to work with late binding with
VB.NET, though, because vb.net supports late binding at language level.


With regards to the late binding example, thank you. It works just as you
said. I had tried a couple of other examples, but they didn't work. From
your example, I expect to be able to build something useful.

Thank you,
Dennis

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hema S | last post by:
Hi, I want to use excel object in my asp.net application. When excel application is installed its working fine. When i uninstal the excel application and refer the relevant dll explicitly... i...
2
by: Darren Barrick via .NET 247 | last post by:
Hello All! I am using Office Automation in one of my applications in orderto read/write to an excel spreadsheet from a VB.Net application. I thus create the application with ExcelApplication...
3
by: Bart Filipski | last post by:
Hello, Does anyone know how looks the syntax of SORT method in Excel object ( i mean precisely in Excel.Workbook.Worksheet.Cells.Sort(...) ) I have tryied in VBA and it works great, but I don't...
3
by: yaya via DotNetMonster.com | last post by:
Hi all, Im using Microsoft Excel 11.0 Object Library as my program reference to create an Excel object, everything works fine but I notice that a process named EXCEL.EXE will run on the backgraound...
3
by: eye5600 | last post by:
I want to read/write Excel files but I am having trouble getting started. Using MS article 302084 as a cookbook, I try to add the reference for MS Excel Object Library. Two versions are listed, 5 &...
4
by: Norton | last post by:
I type the following code to open/close an excel appz Dim oExcel As Excel.Application oExcel = new Excel.Application .... .... ... If Not oExcel Is Nothing Then oExcel.Quit() If Not oExcel...
4
by: Mr.Doubt | last post by:
I've a widows application, that makes use of an EXCEL COM object, to run a few macros available in a EXCEL file. The object is created when the app is started and is closed when the app is closed....
0
by: NDK | last post by:
Ok so I have an html page with an embedded excel object. I load some information into the object from a spreadsheet. (works perfectly) I would like to be able to change the background of a few...
3
by: rlntemp-gng | last post by:
RE: Access 2003/Excel 2003 Problem: After I close the Access application completely, I go out to the Task Manager and there is an Excel.exe object still sitting out there. My Access...
1
by: Scott M. | last post by:
Many methods return objects when they are called. With Excel these objects are placed in memory and must be destroyed via ReleaseComObject as you have done with your NAR method, but the line: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.