Excel COM object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dent2

    #1

    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.FileN otFoundExceptio n: File or assembly name
    "Microsoft.Offi ce.Interop.Exce l, 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.Applicati on() 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


  • Wiktor Zychla

    #2
    Re: Excel COM object

    > System.IO.FileN otFoundExceptio n: File or assembly name[color=blue]
    > "Microsoft.Offi ce.Interop.Exce l, or one of its dependencies, was not[/color]
    found."

    if you use Excel.Applicati on 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.Applicati on 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.GetTypeFro mProgID( "Word.Applicati on" );
    object w = Activator.Creat eInstance( t );

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

    // w.Documents...
    object docs = t.InvokeMember( "Documents" , BindingFlags.Ge tProperty,
    null, w, null );
    // ...Add
    t.InvokeMember( "Add", BindingFlags.In vokeMethod,
    null, docs, null );

    w = null;
    }
    catch( TypeLoadExcepti on ex )
    {
    ...
    }

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



    Comment

    • Dent

      #3
      Re: Excel COM object

      Wiktor,
      [color=blue]
      > if you use Excel.Applicati on 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.[/color]
      that[color=blue]
      > way the early binding will work.[/color]

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

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

      System.Runtime. InteropServices .Marshal.Releas eComObject(Exce lObj);
      ExcelObj = null;
      }
      catch(IOExcepti on e) //not sure about this catch statement. I need
      to study my exception handling.
      {
      ...
      }
      This is the code that generated the "System.IO.File NotFoundExcepti on" 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.d ll and
      microsoft.offic e.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.
      DllRegisterServ er may not be exported, or a corrupt version of
      interop.excel.d ll may be in memory. Consider using pview to detect and
      remove it."

      It was a shot in the dark on my part.
      [color=blue]
      > if you wish to use late binding, you do not have to use
      > Excel.Applicati on 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.[/color]

      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


      Comment

      • Javed

        #4
        Re: Excel COM object

        I have also followed up your late binding code , its great because
        earlier I was creating the ApplicationClas s 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.GetTypeFro mProgID( "Excel.Applicat ion" );
        object w = Activator.Creat eInstance( t );
        // Visible True
        t.InvokeMember( "Visible", BindingFlags.Se tProperty,null, w, new
        Object[] { true } );
        // w WorkBooks
        object docs = t.InvokeMember( "WorkBooks" ,
        BindingFlags.Ge tProperty,null, w, null );
        // w Add method
        t.InvokeMember( "Add", BindingFlags.In vokeMethod,null , docs, null );
        w = null;

        the exception is

        "System.Reflect ion.TargetExcep tion: Object does not match target
        type.\r\n at System.RuntimeT ype.InvokeDispM ethod(String name,
        BindingFlags invokeAttr, Object target, Object[] args, Boolean[]
        byrefModifiers, Int32 culture, String[] namedParameters )\r\n at
        System.RuntimeT ype.InvokeMembe r(String name, BindingFlags invokeAttr,
        Binder binder, Object target, Object[] args, ParameterModifi er[]
        modifiers, CultureInfo culture, String[] namedParameters )\r\n at
        System.Type.Inv okeMember(Strin g name, BindingFlags invokeAttr, Binder
        binder, Object target, Object[] args)\r\n at
        ExcelTry2.Excel InterOp.Init() in c:\\documents and settings\\champ \\my
        documents\\visu al studio
        projects\\excel try2\\exceltry2 \\excelinterop. cs:line 41"


        "Dent" <dtiveljr@hotma il.com> wrote in message news:<uhXZXxLmD HA.1740@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
        > Wiktor,
        >[color=green]
        > > if you use Excel.Applicati on 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.[/color]
        > that[color=green]
        > > way the early binding will work.[/color]
        >
        > I was not able to get my early-binding code to work. In short, here is the
        > code:
        >
        > c#
        > using Microsoft.Offic e.Interop.Excel ;
        > using excel = Microsoft.Offic e.Interop.Excel ;
        > class GetInventory
        > {
        > public static void Main()
        > {
        > try
        > {
        > excel.Applicati on ExcelObj = new excel.Applicati on();
        > ... (working with ExcelObj)
        >
        > System.Runtime. InteropServices .Marshal.Releas eComObject(Exce lObj);
        > ExcelObj = null;
        > }
        > catch(IOExcepti on e) //not sure about this catch statement. I need
        > to study my exception handling.
        > {
        > ...
        > }
        > This is the code that generated the "System.IO.File NotFoundExcepti on" 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.d ll and
        > microsoft.offic e.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.
        > DllRegisterServ er may not be exported, or a corrupt version of
        > interop.excel.d ll may be in memory. Consider using pview to detect and
        > remove it."
        >
        > It was a shot in the dark on my part.
        >[color=green]
        > > if you wish to use late binding, you do not have to use
        > > Excel.Applicati on 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.[/color]
        >
        > 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[/color]

        Comment

        Working...